1, Anonymous internal mode
This method seems simple and easy to understand on the whole, and is commonly used by individuals
xml layout
<LinearLayout 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="com.example.button.MainActivity" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
java code
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find button control Button button = (Button) findViewById(R.id.button); // Setting click event in anonymous internal mode button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Display the prompt Toast on the screen Toast.makeText(MainActivity.this, "Anonymous internal mode", Toast.LENGTH_SHORT).show(); } }); } }
When multiple controls need to implement event listening, it can be seen that anonymous objects are extracted to avoid resource consumption by creating objects multiple times
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1. Find the button control Button button = (Button) findViewById(R.id.button); // 3. Set button click event button.setOnClickListener(onClickListener); } // 2. Get OnClickListener object OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View v) { // You can use switch to set different buttons to prompt different contents by matching the control id // view.getId() gets the id of the clicked control switch (v.getId()) { case R.id.button: Toast.makeText(MainActivity.this, "Different content can be set using", Toast.LENGTH_SHORT).show(); break; case 2: // ellipsis break; default: break; } } }; }
2, Implement OnClickListener interface and rewrite onClick method
This method is suitable for multiple controls to implement click event monitoring
public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1. Find the button control Button button = (Button) findViewById(R.id.button); // 3. Button setting click event button.setOnClickListener(this); } @Override public void onClick(View v) { // 2. Implement the OnClickListener interface and override the onClick method Toast.makeText(MainActivity.this, "Realization OnClickListener", Toast.LENGTH_SHORT).show(); } }
This method can also create a class to implement the OnClickListener interface
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1. Find the button control Button button = (Button) findViewById(R.id.button); // 3. Button setting click event button.setOnClickListener(new myOnclickLister()); } // 2. Create your own class to implement the OnClickListener interface, and then override the onClick method class myOnclickLister implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Realization OnClickListener", Toast.LENGTH_SHORT).show(); } } }
3, OnClick property of XML settings button
<LinearLayout 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="com.example.button.MainActivity" > <Button android:onClick="btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
Then in java code, the method to establish the corresponding name of onClick property
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //Define the method corresponding to the XML OnClick attribute name. Note that add View v to the method parameter public void btn(View v) { Toast.makeText(MainActivity.this, "Set up Onclick attribute", Toast.LENGTH_SHORT).show(); } }
Although this method is very convenient and the code is simple, it is not easy for others to understand the aspect naming problem, and it is not used in multiple controls.