【问题标题】:Handle multiple button click listeners处理多个按钮单击侦听器
【发布时间】:2011-12-02 20:52:58
【问题描述】:

我的布局中有两个 ImageButton,“加号”和“减号”。 我在“加号”按钮上附加了一个点击监听器,在点击函数中带有一条简单的 toast 消息,它可以工作。

    ImageButton btplus = (ImageButton)findViewById(R.id.btplus);        
    btplus.setOnClickListener( new ImageButton.OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "+", Toast.LENGTH_SHORT);
        }
    });

然后我将第二个点击监听器附加到减号 ImageButton...

    ImageButton btminus = (ImageButton)findViewById(R.id.btminus);
    btminus.setOnClickListener( new ImageButton.OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "-", Toast.LENGTH_SHORT);
        }
    });

...没有更多的按钮点击工作! “加号”点击也停止工作。

我看过很多关于这个主题的例子,但我看不出它们(工作)和我的有什么区别。

【问题讨论】:

  • 只需将 ImageButton.onClickListener() 更改为 View.onClickListener() 并在 toast 方法的末尾添加 .show() 。 Like = Toast.makeText(getApplicationContext(), "-", Toast.LENGTH_SHORT).show();
  • @ChiragRaval 您应该将其发布为答案。
  • @Audrius 感谢您的 cmets。我认为如果仅通过添加简单的 cmets 即可解决任何问题,则无需发布答案。

标签: android button listener


【解决方案1】:
 ImageButton btplus = (ImageButton)findViewById(R.id.btplus);        
    btplus.setOnClickListener( new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "+", Toast.LENGTH_SHORT).show();
        }
    });

【讨论】:

    【解决方案2】:

    如多按钮点击监听器...

     ImageButton btminus = (ImageButton)findViewById(R.id.btminus);
     btminus.setOnClickListener(this);
    
     ImageButton btplus = (ImageButton)findViewById(R.id.btplus);
     btplus.setOnClickListener(this);
    

    那么您的活动想要实现 onclicklistener 并且您实现的方法是:

        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btplus:
                Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show(); 
                break;
            case R.id.btminus:
                Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
            }
        }
    

    【讨论】:

    • 谢谢,我知道这种方法,但我更喜欢保留单独的功能:我只需要那个“.show()”
    【解决方案3】:

    在 XML 布局中注册 onClick 事件,然后在代码中处理。我会这样做:

    <Button
    android:id="@+id/btplus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:onClick="onBtnClicked">
    </Button>
    

    .class 中的方法

     public void onBtnClicked(View v) {
            switch (v.getId()) {
            case R.id.btplus:
                Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show(); 
                break;
            case R.id.btminus:
                Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 2015-07-31
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      相关资源
      最近更新 更多