UiUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.IBinder; import android.os.Looper; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.ImageView; import java.io.InputStream; /** * UI工具类 * @version 1.0 */ public class UiUtilities { /** * 设置view的显示状态 */ public static void setVisibilitySafe(View view, int visibility) { if (view != null && view.getVisibility() != visibility) { view.setVisibility(visibility); } } /** * 设置view的显示状态 */ public static void setVisibilitySafe(View parent, int id, int visibility) { if (parent != null ) { setVisibilitySafe(parent.findViewById(id), visibility); } } public static void setPressedSafe(View view, boolean pressed) { if (view != null && view.isPressed() != pressed) { view.setPressed(pressed); } } public static void setEnabledSafe(View parent, int id, boolean enabled) { if (parent != null ) { View view = parent.findViewById(id); if (view != null ) { view.setEnabled(enabled); } } } public static void setOnClickListenerSafe(View parent, int id, OnClickListener l) { if (parent != null ) { View view = parent.findViewById(id); if (view != null ) { view.setOnClickListener(l); } } } public static void requestFocus(View view) { if (view != null ) { view.setFocusable( true ); view.setFocusableInTouchMode( true ); view.requestFocus(); } } public static boolean isEditTextEmpty(EditText edit) { return edit.getText() == null || edit.getText().toString().trim().length() <= 0 ; } public static boolean hideInputMethod(Activity activity) { return hideInputMethod(activity, activity.getWindow().getDecorView().getWindowToken()); } public static boolean hideInputMethod(Dialog dialog) { return hideInputMethod(dialog.getContext(), dialog.getWindow().getDecorView().getWindowToken()); } public static boolean hideInputMethod(Context context, IBinder iBinder) { InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); return im.hideSoftInputFromWindow(iBinder, 0 ); } public static void checkBackgroudThread() { if (Looper.getMainLooper() == Looper.myLooper()) { throw new IllegalStateException( "It must run in backgroud thread." ); } } public static void cancelAsyncTask(AsyncTask<?, ?, ?> task) { if (task != null ) { task.cancel( true ); } } public static void clearBitmapInImageView(ImageView v) { if (v != null ) { clearBitmapInDrawable(v.getDrawable()); } } public static void clearBackgroundBitmapInView(View v) { if (v != null ) { clearBitmapInDrawable(v.getBackground()); } } public static void clearBitmapInDrawable(Drawable d) { if (d != null && d instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable) d).getBitmap(); if (bitmap != null ) { Logger.v( "luochun" , bitmap.toString()); bitmap.recycle(); } } } public static Bitmap decodeResourceBitmap(Context context, int resId) { InputStream is = context.getResources().openRawResource(resId); return BitmapFactory.decodeStream(is); } } |
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步