下面列出了怎么用androidx.annotation.IdRes的API类实例代码及写法,或者点击链接到github查看源代码。
public void setChecked(@IdRes int checkedId, boolean doApply) {
mRadioGroup.setOnCheckedChangeListener(null);
for (int i=0; i<mRadioGroup.getChildCount(); i++) {
RadioButton button = (RadioButton) mRadioGroup.getChildAt(i);
if (i == checkedId) {
button.setChecked(true);
} else {
button.setChecked(false);
}
}
mRadioGroup.setOnCheckedChangeListener(mInternalRadioListener);
if (mRadioGroupListener != null && doApply) {
mRadioGroupListener.onCheckedChanged(mRadioGroup, checkedId, doApply);
}
}
@Nullable
private Integer readIntFromTextView(@IdRes int resId) {
Integer intValue = null;
View view = findViewById(resId);
if (view instanceof TextView) {
CharSequence contents = ((TextView) view).getText();
if (!TextUtils.isEmpty(contents)) {
try {
intValue = Integer.parseInt(contents.toString());
} catch (NumberFormatException e) {
showErrorAlert(R.string.error_alert_message_invalid_photo_size);
}
}
}
return intValue;
}
@Nullable
private Integer readIntFromTextView(@IdRes int resId) {
Integer intValue = null;
View view = findViewById(resId);
if (view instanceof TextView) {
CharSequence contents = ((TextView) view).getText();
if (!TextUtils.isEmpty(contents)) {
try {
intValue = Integer.parseInt(contents.toString());
} catch (NumberFormatException e) {
showErrorAlert(R.string.error_alert_message_invalid_photo_size);
}
}
}
return intValue;
}
@SuppressLint("ResourceType")
@NonNull
public static ProgressDialog getDialog(
@NonNull Context context,
@IdRes int textMessageResId
) {
android.app.ProgressDialog progressDialog
= new android.app.ProgressDialog(context);
progressDialog.setMessage(
context.getString(
textMessageResId
)
);
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.getWindow().setBackgroundDrawable(
new ColorDrawable(
UIUtils.getCommonBackgroundColor(
context
)
)
);
return progressDialog;
}
/**
* Returns the shared element according to the given id.
*
* @param resultCode The transition result code.
* @param position The position of the shared element.
* @param viewId The id resource to find the view by id.
*
* @return The view according to the view id.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected @Nullable View getSharedElement(int resultCode, int position,
@NonNull String transition, @IdRes int viewId) {
View view = mDynamicTransitionListener == null ? findViewById(viewId)
: mDynamicTransitionListener.onFindView(resultCode, position, transition, viewId);
if (view != null) {
view.setTag(null);
}
return view;
}
private static @NonNull View getChildOrThrow(@NonNull View parent, @IdRes int id) {
View child = parent.findViewById(id);
if (child == null) {
throw new AssertionError("Can't find view with ID " + R.id.avatar_target);
} else {
return child;
}
}
static Feeling getByViewId(@IdRes int viewId) {
for (Feeling feeling : values()) {
if (feeling.viewId == viewId) {
return feeling;
}
}
throw new AssertionError();
}
/**
* 根据 id 查找 View
*/
@Override
public <V extends View> V findViewById(@IdRes int id) {
if (mContentView == null) {
// 没有 setContentView 就想 findViewById ?
throw new IllegalStateException("are you ok?");
}
return mContentView.findViewById(id);
}
/**
* Show the view with {@code viewId} id and hide all others
*/
public void setShownView(@IdRes int viewId) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.setVisibility(child.getId() == viewId ? VISIBLE : GONE);
}
}
/**
* 初始化 View
* @param activity {@link Activity}
* @param id R.id.viewId
* @param <T> 泛型
* @return {@link View}
*/
public static <T extends View> T findViewById(final Activity activity, @IdRes final int id) {
if (activity != null) {
try {
return activity.findViewById(id);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "findViewById");
}
}
return null;
}
/**
* 设置长按事件
* @param activity {@link Activity}
* @param onLongClickListener {@link View.OnLongClickListener}
* @param viewIds View id 数组
* @return {@code true} success, {@code false} fail
*/
public static boolean setOnLongClicks(final Activity activity, final View.OnLongClickListener onLongClickListener, @IdRes final int... viewIds) {
if (activity != null && onLongClickListener != null && viewIds != null) {
for (int i = 0, len = viewIds.length; i < len; i++) {
View findView = ViewUtils.findViewById(activity, viewIds[i]);
if (findView != null) {
findView.setOnLongClickListener(onLongClickListener);
}
}
return true;
}
return false;
}
public Bitmap getSrcBitmap(Resources resources, @IdRes int resId) {
String path = String.valueOf(resId);
Bitmap bitmap = srcBitmapCache.get(path);
if (null == bitmap) {
bitmap = BitmapFactory.decodeResource(resources, resId);
srcBitmapCache.put(path, bitmap);
bitmapUsedCount.put(path, 0);
convertMirror(path, bitmap);
}
int count = bitmapUsedCount.get(path);
bitmapUsedCount.put(path, ++count);
return bitmap;
}
private void toggleIvMenu(@IdRes int resId) {
for (ImageView ivMenu : ivMenus) {
if (ivMenu.getId() == resId) {
ivMenu.setColorFilter(ContextCompat.getColor(this, R.color.easy_photos_fg_accent));
} else {
ivMenu.clearColorFilter();
}
}
}
/**
* 设置下一个获取焦点的 View id
* @param view {@link View}
* @param nextFocusForwardId 下一个获取焦点的 View id
* @return {@link View}
*/
public static View setNextFocusForwardId(final View view, @IdRes final int nextFocusForwardId) {
if (view != null) {
view.setNextFocusForwardId(nextFocusForwardId);
}
return view;
}
/**
* Sets file name in the text field of a dialog.
*
* @param alertDialog the dialog
* @param destinationName the id of the EditText
* @param name the name
*/
public void presetNameForDialog(AlertDialog alertDialog, @IdRes int destinationName, String name) {
EditText editText = alertDialog.findViewById(destinationName);
if (editText != null) {
editText.setText(name);
} else {
Log.w(TAG, "Unable to find view, can not set file title.");
}
}
/**
* 通过状态栏高度动态设置状态栏布局,只能在Activity中使用
*
* @param viewId the view id
* @return the immersion bar
*/
public ImmersionBar statusBarView(@IdRes int viewId) {
View view = mActivity.findViewById(viewId);
if (view == null) {
throw new IllegalArgumentException("未找到viewId");
}
return statusBarView(view);
}
/**
* 解决状态栏与布局顶部重叠又多了种方法,只支持Activity
* Title bar immersion bar.
*
* @param viewId the view id
* @return the immersion bar
*/
public ImmersionBar titleBar(@IdRes int viewId) {
View view = mActivity.findViewById(viewId);
if (view == null) {
throw new IllegalArgumentException("参数错误");
}
return titleBar(view, true);
}
/**
* Title bar immersion bar.
*
* @param viewId the view id
* @param statusBarFlag the status bar flag
* @return the immersion bar
*/
public ImmersionBar titleBar(@IdRes int viewId, boolean statusBarFlag) {
View view = mActivity.findViewById(viewId);
if (view == null) {
throw new IllegalArgumentException("参数错误");
}
return titleBar(view, statusBarFlag);
}
/**
* Title bar immersion bar.
*
* @param viewId the view id
* @param rootView the root view
* @return the immersion bar
*/
public ImmersionBar titleBar(@IdRes int viewId, View rootView) {
View view = rootView.findViewById(viewId);
if (view == null) {
throw new IllegalArgumentException("参数错误");
}
return titleBar(view, true);
}
private void setNavigationCheckedItem(@IdRes int itemId) {
MenuItem item = mNavigationView.getCheckedItem();
if (item != null && item.getItemId() == itemId) {
return;
}
Fragment fragment;
switch (itemId) {
case R.id.recycler_view_list:
fragment = RecyclerViewListFragment.newInstance();
break;
case R.id.recycler_view_list_classic:
fragment = RecyclerViewListClassicFragment.newInstance();
break;
case R.id.recycler_view_list_stateful:
fragment = RecyclerViewListStatefulFragment.newInstance();
break;
case R.id.recycler_view_grid:
fragment = RecyclerViewGridFragment.newInstance();
break;
case R.id.scroll_view:
fragment = ScrollViewFragment.newInstance();
break;
case R.id.nested_scroll_view:
fragment = NestedScrollViewFragment.newInstance();
break;
case R.id.web_view:
fragment = WebViewFragment.newInstance();
break;
default:
throw new AssertionError(itemId);
}
getChildFragmentManager().beginTransaction()
.replace(R.id.content, fragment)
.commit();
mNavigationView.setCheckedItem(itemId);
}
/**
* @param resId 0 for clear
*/
public void setNavCheckedItem(@IdRes int resId) {
FragmentActivity activity = getActivity();
if (activity instanceof MainActivity) {
((MainActivity) activity).setNavCheckedItem(resId);
}
}
/**
* 设置向左移动焦点时, 下一个获取焦点的 View id
* @param view {@link View}
* @param nextFocusLeftId 下一个获取焦点的 View id
* @return {@link View}
*/
public static View setNextFocusLeftId(final View view, @IdRes final int nextFocusLeftId) {
if (view != null) {
view.setNextFocusLeftId(nextFocusLeftId);
}
return view;
}
/**
* 解决状态栏与布局顶部重叠又多了种方法,只支持Activity
* Title bar immersion bar.
*
* @param viewId the view id
* @return the immersion bar
*/
public ImmersionBar titleBar(@IdRes int viewId) {
View view = mActivity.findViewById(viewId);
if (view == null) {
throw new IllegalArgumentException("参数错误");
}
return titleBar(view, true);
}
/**
* Title bar immersion bar.
*
* @param viewId the view id
* @param statusBarFlag the status bar flag
* @return the immersion bar
*/
public ImmersionBar titleBar(@IdRes int viewId, boolean statusBarFlag) {
View view = mActivity.findViewById(viewId);
if (view == null) {
throw new IllegalArgumentException("参数错误");
}
return titleBar(view, statusBarFlag);
}
private static int getColorFromAttrs(
@NonNull Context context,
@IdRes int themeColor
) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(
themeColor,
typedValue,
true
);
return typedValue.data;
}
public <T extends View> T getView(@IdRes int id) {
if (id == View.NO_ID) {
throw new RuntimeException("id is invalid");
}
View view = views.get(id);
if (view == null) {
view = itemView.findViewById(id);
views.put(id, view);
}
return (T) view;
}
/**
* 设置点击事件
*/
public B setOnClickListener(@IdRes int id, @NonNull BaseDialog.OnClickListener listener) {
if (isCreated()) {
View view = mDialog.findViewById(id);
if (view != null) {
view.setOnClickListener(new ViewClickWrapper(mDialog, listener));
}
} else {
if (mClickArray == null) {
mClickArray = new SparseArray<>();
}
mClickArray.put(id, listener);
}
return (B) this;
}
@Override
public void onFieldError(@IdRes int fieldId, String reason) {
if (layout == null) return;
TextInputLayout inputLayout = layout.findViewById(fieldId);
if (inputLayout != null) {
inputLayout.setErrorEnabled(true);
inputLayout.setError(reason);
}
}
/**
* 设置图片
*/
public B setImageDrawable(@IdRes int viewId, @DrawableRes int drawableId) {
return setBackground(viewId, ContextCompat.getDrawable(mContext, drawableId));
}
private ViewRef(@IdRes int idRes) {
this.idRes = idRes;
}