Four common ways to write Android button click events are summarized

  • 2020-06-03 08:28:13
  • OfStack

Many people who study Android programming will find that everyone has a different preference for how their code is written, especially when it comes to the way controls respond to events. Therefore, this paper summarizes these methods and compares the advantages and disadvantages of various methods, hoping to have a definite reference value for us to choose the coding method flexibly.

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 as follows:

Anonymous inner classes:


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();
      }
    });
  }
}

Custom click event listener 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 Activity implements OnClick(View view) method. In OnClick(View view) method, switch-ES27en is used to deal with button represented by different id


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 the one I saw today. In the XML file, "display the onClick attribute of the specified button, so that when the button is clicked, the corresponding click() method in Activity is 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 Typing android:, press Alt+/ and there will be a prompt for onClick attribute. However, when typing android:onClick= ", press Alt+/ and there is no prompt for onClick option.


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);
  }

  //  Pay attention to   There is no  @Override  The label 
  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 button's clicks without declaring button throughout the code.

These are the four ways to implement button click events.

As a rough summary, using anonymous inner classes is faster when there are fewer buttons, such as when writing demo tests or logging into the interface.

In the case of too many buttons, I still choose the third method for convenience.

As for 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. Those who are interested can study it. I believe there will be a lot to gain.

I hope this article is helpful for you to learn Android programming.


Related articles: