1. Implement in xml:
add android:onClick=”button” to the attribute of the button, and then implement it in the code: public void button(View view)
2. Implement by interface:
…implements (1)DialogInterface.OnClickListener (2)View.OnClickListener , the automatically generated methods correspond to public void onClick(DialogInterface dialog, int which) and public void onClick(View v) respectively, the former is A listener for the dialog, which is a listener for the view buttons.
3. Anonymous inner class implementation:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
4. Independent class implementation:
onCreate方法里面:
findViewById(R.id.button).setOnClickListener(myOnClick);
onCreate方法外面:
View.OnClickListener myOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};