shape的使用并完成按钮机制

本文介绍了在Android开发中处理按钮点击事件的三种方法:使用匿名内部类、自定义单击事件监听类及由Activity实现OnClickListener接口。每种方法都有详细的代码示例,帮助开发者更好地理解和应用。
摘要由CSDN通过智能技术生成

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;

      }

    }

  }

 

  @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 view)方法,在OnClick(View view)方法中用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;

    }

  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值