Android按钮单击事件的四种写法

XML文件如下:

<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" />

四种方法分述如下:

1、匿名内部类

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

2、自定义单击事件

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;
      }
    }http://www.dever.top/wp-admin/post-new.php
  }
  
  @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());
  }
}

3、Activity继承View.OnClickListener,由Activity实现OnClick(View v)方法,在OnClick(View v)方法中用switch-case对不同id代表的button进行相应的处理

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

4、在XML文件中对指定按钮的onClick属性设置相同的onClick值,这样点击按钮会利用反射的方式调用对应Activity中的clicl()方法

XML代码:

<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" />

Android代码:

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

这种写法整个代码都不用声明button就可以实现button的单击事件。

posted @ 2015-11-14 21:04  wmkill  阅读(209)  评论(0编辑  收藏  举报