-
- Button Btn1 = (Button)findViewById(R.id.button1);
- Btn1.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v) {
- String strTmp = "点击Button01";
- Ev1.setText(strTmp);
- }
-
- });
-
- public void Btn3OnClick(View view){
- String strTmp="点击Button03";
- Ev1.setText(strTmp);
-
- }
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button3"
- android:onClick="Btn3OnClick"/>
第三种方式 activity 实现 单击监听接口
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) {
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
;
}
}
}