Button click event generally has four methods:
1: Anonymous inner class
2: Custom inner class
3: Implement the click event interface through the current Activity
4: Bind in the xml file
Anonymous inner class format:
<Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="custom click event"></Button>
Add id processing to the button attribute in the xml file
Anonymous inner class format:
Button bt1=findViewById(R.id.bt1); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } });
Custom inner class format:
/ custom inner class Button bt1=findViewById(R.id.bt1); MyClickListener mc=new MyClickListener(); bt1.setOnClickListener(mc); } class MyClickListener implements View.OnClickListener { @Override public void onClick(View view) { } }
The current Activity implements the click event interface:
Make the current activity implement this interface
Button bt1=findViewById(R.id.bt1); bt1.setOnClickListener(this); } @Override public void onClick(View view) { }
Just override this method
Bind in xml file:
android:onClick="Myclick"
Call the onclick attribute, the latter value is the method to be bound, you need to rewrite it yourself
public void Myclick(View v) {
switch (v.getId()) {
case R.id.bt1:
break;
case R.id.bt2:
break;
}
}
Control multiple buttons at the same time by matching switch and id
// Jump to the page // Intent intent=new Intent(NowActivity.this,NewActivity.class); // startActivity(intent);