버튼 처리 첫번째
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnTest).setOnClickListener(mClickListener); findViewById(R.id.btnTest2).setOnClickListener(mClickListener); } Button.OnClickListener mClickListener = new View.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.btnTest: Toast.makeText(getBaseContext(), "DDDDD", 0).show(); break; case R.id.btnTest2: Toast.makeText(getBaseContext(), "EEEEE", 0).show(); break; } } };
버튼 처리 두번째
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnTest = (Button)findViewById(R.id.btnTest); btnTest.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), "RRRRRRR", 0).show(); } }); }
버튼 처리 세번째
레이아웃 XML 파일내용
<Button android:id="@+id/btnTest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btnMSG_01" android:onClick="testMethod" />
자바 코드 내용
public void testMethod(View v){ Toast.makeText(getBaseContext(), "GGGGGGG", 0).show(); }
댓글 없음 :
댓글 쓰기