标签:
xml 代码:
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Button1"
/>
<
Button
android:id
=
"@+id/button2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Button2"
/>
public
class
TestButtonActivity
extends
Activity {
Button btn1, btn2;
Toast tst;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(TestButtonActivity.
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
}
});
btn2.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(TestButtonActivity.
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
}
});
}
}
public
class
TestButtonActivity
extends
Activity {
Button btn1, btn2;
Toast tst;
class
MyClickListener
implements
OnClickListener {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.button1:
tst = Toast.makeText(TestButtonActivity.
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
break
;
case
R.id.button2:
tst = Toast.makeText(TestButtonActivity.
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
break
;
default
:
break
;
}
}
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(
new
MyClickListener());
btn2.setOnClickListener(
new
MyClickListener());
}
}
public
class
TestButtonActivity
extends
Activity
implements
OnClickListener {
Button btn1, btn2;
Toast tst;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(
this
);
btn2.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.button1:
tst = Toast.makeText(
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
break
;
case
R.id.button2:
tst = Toast.makeText(
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
break
;
default
:
break
;
}
}
}
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:onClick
=
"onClick"
android:text
=
"Button1"
/>
<
Button
android:id
=
"@+id/button2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:onClick
=
"onClick"
android:text
=
"Button2"
/>
public
class
TestButtonActivity
extends
Activity {
Button btn1, btn2;
Toast tst;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
}
// 注意 这里没有 @Override 标签
public
void
onClick(View v) {
// TODO Auto-generated method stub
switch
(v.getId()) {
case
R.id.button1:
tst = Toast.makeText(
this
,
"111111111"
, Toast.LENGTH_SHORT);
tst.show();
break
;
case
R.id.button2:
tst = Toast.makeText(
this
,
"222222222"
, Toast.LENGTH_SHORT);
tst.show();
break
;
default
:
break
;
}
}
}
按钮少的时候用匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类的。
按钮多的情况我还是选择第三种方法,方便。
关于第四种方法,我感觉最方便,但看了很多代码还是觉得写法不够大众化,感兴趣的朋友可以对此研究研究。
标签:
原文地址:http://www.cnblogs.com/weixuexi/p/4689929.html