본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] findViewById casting bolierplate removal.

by 돼지왕 왕돼지 2014. 4. 17.
반응형


 android, findViewById casting bolierplate removal.

 

[android] findViewById bolierplate removal.


<boilerplate casting removal>

@SuppressWarnings("unchecked")

public final <E extends View> E findView (int id) {

    return (E) findViewById(id);

}



<사용방법>

@Override

public void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


TextView textView = findView(R.id.textview);

Button button = findView(R.id.button);

ImageView image = findView(R.id.imageview);


    myMethod( findView(R.id.imageview) ); //Will not compile

    myMethod( (ImageView) findView(R.id.imageview) ); //Compiles

}


private static void myMethod ( ImageView img ) {

    //Do nothing

}






반응형

댓글