Many people who study Android programming will find that everyone has different preferences for how to write code. The most obvious one is the different way of writing controls to respond to events. Therefore, this article will summarize these writing methods and compare the advantages and disadvantages of various writing methods. I hope it can be of certain reference value for everyone to flexibly choose encoding methods.
The xml file code is as follows:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" />
The four methods are described as follows:
Anonymous internal class:
public class TestButtonActivity extends Activity { Button btn1, btn2; Toast tst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT); tst.show(); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT); tst.show(); } }); } }
Customized click event listening class:
public class TestButtonActivity extends Activity { Button btn1, btn2; Toast tst; class MyClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT); tst.show(); break; case R.id.button2: tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT); tst.show(); break; default: break; } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(new MyClickListener()); btn2.setOnClickListener(new MyClickListener()); } }
Activity inherits View.OnClickListener, and the Activity implements the OnClick(View view) method. In the OnClick(View view) method, switch-case is used to process the buttons represented by different IDs accordingly
public class TestButtonActivity extends Activity implements OnClickListener { Button btn1, btn2; Toast tst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT); tst.show(); break; case R.id.button2: tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT); tst.show(); break; default: break; } } }
The last one is a way of writing that I saw today. In the XML file, "display the onClick attribute of the specified button, so that when the button is clicked, the click() method in the corresponding Activity will be called by reflection"
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button2" />
Here, when I press Alt+/ after typing android:, there will be a prompt for the onClick attribute. However, when I press Alt+/ after typing android:onClick="", there is no prompt for the onClick option, which suddenly made me feel like there is something wrong here. .
public class TestButtonActivity extends Activity { Button btn1, btn2; Toast tst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); } // 注意 这里没有 @Override 标签 public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT); tst.show(); break; case R.id.button2: tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT); tst.show(); break; default: break; } } }
This way of writing can realize the button click event without declaring the button.
The above are four ways to implement the button click event. #A rough summary is that it is faster to use anonymous internal classes when there are few buttons, such as when writing demo tests or login interfaces.
When there are many buttons, I still choose the third method, which is more convenient. .
Regarding the fourth method, I think it is the most convenient, but after reading a lot of code, I still feel that the writing method is not popular enough. I believe that there will be a lot of gains from studying this.
I hope this article will be helpful to everyone's learning of Android programming
For more related articles on the four common methods of writing Android button click events, please pay attention to the PHP Chinese website