8/19/2013

안드로이드 많은 버튼들 깔끔하게 하기

이게 리팩토링(refactoring)의 기초임


Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<TextView
   android:id="@+id/fruit"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#ffff00"
   android:textSize="40sp"
   android:text="Fruit"
   />
<LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="fill_parent"
   >
   <Button
       android:id="@+id/apple"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Apple"
       />
   <Button
       android:id="@+id/orange"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Orange"
       />
 
</LinearLayout>

</LinearLayout>



Old code:

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnApple = (Button)findViewById(R.id.apple);
btnApple.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
TextView textFruit = (TextView)findViewById(R.id.fruit);
textFruit.setText("Apple");
}
});

Button btnOrange = (Button)findViewById(R.id.orange);
btnOrange.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
TextView textFruit = (TextView)findViewById(R.id.fruit);
textFruit.setText("Orange");
}
});
}
}



New code:

public class MainActivity extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnApple = (Button)findViewById(R.id.apple);
btnApple.setOnClickListener(this);

Button btnOrange = (Button)findViewById(R.id.orange);
btnOrange.setOnClickListener(this);

}

public void onClick(View v)
{
TextView textFruit = (TextView)findViewById(R.id.fruit);
switch (v.getId())
{
case R.id.apple:
textFruit.setText("Apple");
break;
case R.id.orange:
textFruit.setText("Orange");
break;
}
}
}


Better:

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.apple).setOnClickListener(mClickListener);
findViewById(R.id.orange).setOnClickListener(mClickListener);
}

Button.OnClickListener mClickListener = new View.OnClickListener()
{
public void onClick(View v)
{
TextView textFruit = (TextView)findViewById(R.id.fruit);
switch (v.getId())
{
case R.id.apple:
textFruit.setText("Apple");
break;
case R.id.orange:
textFruit.setText("Orange");
break;
}
}
};
}


그리고 SDK 1.6에서 새로 추가된 클릭 이벤트 처리 방법

Layout:

버튼에다가 아래 항목 추가

android:onClick="mOnClick"


Code:

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void mOnClick(View v)
{
TextView textFruit = (TextView)findViewById(R.id.fruit);
switch (v.getId())
{
case R.id.apple:
textFruit.setText("Apple");
break;
case R.id.orange:
textFruit.setText("Orange");
break;
}
}
}

출처: 안드로이드 프로그래밍 정복 1권

No comments:

Post a Comment