第一种方式
在onCreate(Bundle savedInstanceState)方法中写:
Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源
Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
public void onClick(View v) {
String strTmp = "点击Button01";
Ev1.setText(strTmp);
}
});
第二种方式
在onCreate(Bundle savedInstanceState)方法中写:
Button Btn2 = (Button) findViewById(R.id.button2);//获取按钮资源
Btn2.setOnClickListener(listener);//设置监听
Button.OnClickListener listener = new Button.OnClickListener(){//创建监听对象
public void onClick(View v){
String strTmp="点击Button02";
Ev1.setText(strTmp);
}
};
第三种方式(Android1.6版本及以后的版本中提供了)
1. JAVA代码
public void Btn3OnClick(View view){
String strTmp="点击Button03";
Ev1.setText(strTmp);
}
在xml中
2. 最外部的布局加此代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResetPwdActivity"
android:background="@color/white">
3. 然后在控件中加onClick事件
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:onClick="Btn3OnClick"/>