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);
   }
}

 

posted on   LoaderMan  阅读(214)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示