Advertisement
fslasht

WidgetBinderUty.java

Oct 22nd, 2013
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.06 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.Modifier;
  3.  
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. /**
  13.  * Activityのウィジットを便利に使うユーティリティ
  14.  * @author fslasht
  15.  *
  16.  */
  17. public class WidgetBinderUty {
  18.     static private String TAG ="WidgetBinder";
  19.  
  20.  
  21.     /**
  22.      * Activityのメンバー変数のうちウィジットクラスに対して、レイアウト上の同名のIDのウィジットを自動的にセットする
  23.      * @param activity  対象Activity
  24.      */
  25.     static public void bindWidgetToMemberVar(Activity activity) {
  26.         View decor = activity.getWindow().getDecorView();
  27.         bindWidgetToMemberVar(activity.getApplicationContext(),activity,decor);
  28.     }
  29.  
  30.     static public void bindWidgetToMemberVar(Context context,Object obj , View viewTop) {
  31.  
  32.         for (Field field : obj.getClass().getDeclaredFields()) {
  33.             if ( !Modifier.isStatic(field.getModifiers() )) {
  34.                 String name = field.getName();
  35.                 if ( name.endsWith("_")) name = name.substring(0, name.length()-1); // 末尾に「_」がある場合除外
  36.  
  37.                 field.setAccessible(true);  // privateでもアクセス出来るようにする
  38.                 try {
  39.  
  40.                     if (field.getType() == TextView.class) {
  41.                         TextView v = (TextView)(getView(context,viewTop,name));
  42.                         field.set(obj,v );
  43.                     } else if (field.getType() == Button.class) {
  44.                         Button v = (Button)(getView(context,viewTop,name));
  45.                         field.set(obj,v );
  46.                     } else if (field.getType() == EditText.class) {
  47.                         EditText v = (EditText)(getView(context,viewTop,name));
  48.                         field.set(obj,v );
  49.                     }
  50.                     // 利用するウィジットの種類の分追加する
  51.  
  52.                 } catch (IllegalArgumentException e) {
  53.                     e.printStackTrace();
  54.                 } catch (IllegalAccessException e) {
  55.                     e.printStackTrace();
  56.                 }catch ( ClassCastException e) {
  57.                     e.printStackTrace();
  58.                 }
  59.             }
  60.         }
  61.  
  62.     }
  63.  
  64.  
  65.     // uty
  66.     static private View getView(Context context,View viewTop , String idName) {
  67.         int id = getResourceIDfromString(context, idName);
  68.         return (viewTop.findViewById(id));
  69.     }
  70.  
  71.     // ID取得
  72.     static public int getResourceIDfromString(Context context , String name) {
  73.         return context.getResources().getIdentifier(name, "id", context.getPackageName());
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. /* 使用例
  80. public class MainActivity extends Activity {
  81.     // widgets
  82.     Button buttonCreateBitmap_;
  83.     EditText edittextWidth_;
  84.     EditText edittextHeight_;
  85.     TextView textviewResult_;
  86.  
  87.  
  88.  
  89.     @Override
  90.     protected void onCreate(Bundle savedInstanceState) {
  91.         super.onCreate(savedInstanceState);
  92.         setContentView(R.layout.activity_main);
  93.  
  94.         // bind
  95.         WidgetBinderUty.bindWidgetToMemberVar(this);    // ★ココ!
  96.  
  97.         // event
  98.         edittextWidth_.setText("100AA");
  99.         edittextHeight_.setText("100");
  100.  
  101.         buttonCreateBitmap_.setOnClickListener(new OnClickListener() {
  102.             @Override
  103.             public void onClick(View v) {
  104.                 textviewResult_.setText(edittextWidth_.getText());
  105.             }
  106.         });
  107.  
  108.  
  109.     }
  110. }
  111. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement