android中click事件触发机制
1、在XML直接指定click事件名称
<Button android:text="点击"
android:id="@+id/bt_Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnOnClick"</span>>
</Button>
Activity中定义方法
public void btnOnClick(View view){
Intent intent = new Intent(mainActivity.this, fristActivity.class);
intent.putExtra("data", "mainActivity");
startActivity(intent);
}
2、Activity中定义OnClickListener接口监听事件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.bt_Click).setOnClickListener(onclick);
}
OnClickListener onclick = new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(mainActivity.this, fristActivity.class);
intent.putExtra("data", "mainActivity");
startActivity(intent);
}
};
3、在Activity为button指定绑定操作
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.bt_Click).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(mainActivity.this, fristActivity.class);
intent.putExtra("data", "mainActivity");
startActivity(intent);
}
});
}