Android Button获取以及点击事件响应

获取Activity中配置文件中的按钮,以及设置按钮的单击事件:

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

posted @ 2020-03-16 10:25  opencoder  阅读(1184)  评论(0编辑  收藏  举报