多个按钮`OnClickListener()`机器人 [英] Multiple Buttons `OnClickListener()` android

查看:162
本文介绍了多个按钮`OnClickListener()`机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在在Android上一个简单的计算器应用程序。我试着去建立code,这样,当一个数字键为pressed它更新的计算器屏幕,这个数字。目前,我做的这样。

 按钮一次=(按钮)findViewById(R.id.oneButton);
    one.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            TextView的输出=(TextView中)findViewById(R.id.output);
            output.append(1);
        }
    });
 

它的工作原理,但我在写这同样$ C $下在计算器上的每一个按键。你可以想象这是非常多余。反正我有可以以更有效的方式写这篇code?其中,涉及不写这个方法为每一个按钮?

解决方案

您只需简单地必须按照以下步骤使其易于...

你不必编写新的 onClickListener 对于每一个按钮 ...只要实施 OnClickLister 活动 / 片段 ..这将实施新的方法叫做的onClick()处理的onClick事件的按钮,TextView`等。

  
      
  1. 实施 OnClickListener()活动 / 片段
  2.   

 公共类MainActivity扩展活动实现OnClickListener {

}
 

  <醇开始=2>   
  • 实现的onClick()方法,在你的活动/片段
  •   

     公共类MainActivity扩展活动实现OnClickListener {
    
        @覆盖
        保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
        }
    
        @覆盖
        公共无效的onClick(视图v){
          //处理的onClick事件默认方法..
        }
    
    }
     

      <醇开始=3>   
  • 实施 OnClickListener()对于按钮
  •   

      @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
    
        的setContentView(R.layout.your_layout);
    
        按钮一次=(按钮)findViewById(R.id.oneButton);
        one.setOnClickListener(本); //调用的onClick()方法
        两个按钮=(按钮)findViewById(R.id.twoButton);
        two.setOnClickListener(本);
        三个按钮=(按钮)findViewById(R.id.threeButton);
        three.setOnClickListener(本);
    
    }
     

      <醇开始=4>   
  • 查找按钮按ID和实施code ..
  •   

      @覆盖
    公共无效的onClick(视图v){
    
        开关(v.getId()){
    
        案例R.id.oneButton:
            //做你的code
            打破;
    
        案例R.id.twoButton:
            //做你的code
            打破;
    
        案例R.id.threeButton:
            //做你的code
            打破;
    
        默认:
            打破;
        }
    
    }
     

    这将能够更方便地处理多个按钮点击事件,使得它看起来简单,它...

    I'm currently making a simple calculator app on Android. Im trying to set up the code so that when a number button is pressed it updates the calculator screen with that number. Currently I'm doing it like this.

        Button one = (Button) findViewById(R.id.oneButton);
        one.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                TextView output = (TextView) findViewById(R.id.output);
                output.append("1");
            }
        });
    

    It works but I'm writing this same code for every single button on the calculator. As you can imagine it is very redundant. Is there anyway I can write this code in a more efficient way? One that involves not writing this method for every single button?

    解决方案

    You Just Simply have to Follow these steps for making it easy...

    You dont have to write new onClickListener for Every Button... Just Implement OnClickLister to your Activity/Fragment.. it will implement new Method called onClick() for handling onClick Events for Button,TextView` etc.

    1. Implement OnClickListener() in your Activity/Fragment

    public class MainActivity extends Activity implements OnClickListener {
    
    }
    

    1. Implement onClick() method in your Activity/Fragment

    public class MainActivity extends Activity implements OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public void onClick(View v) {
          // default method for handling onClick Events..
        }
    
    }
    

    1. Implement OnClickListener() For Buttons

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.your_layout);
    
        Button one = (Button) findViewById(R.id.oneButton);
        one.setOnClickListener(this); // calling onClick() method
        Button two = (Button) findViewById(R.id.twoButton);
        two.setOnClickListener(this);
        Button three = (Button) findViewById(R.id.threeButton);
        three.setOnClickListener(this);
    
    }
    

    1. Find Buttons By Id and Implement Your Code..

    @Override
    public void onClick(View v) {
    
        switch (v.getId()) {
    
        case R.id.oneButton:
            // do your code
            break;
    
        case R.id.twoButton:
            // do your code
            break;
    
        case R.id.threeButton:
            // do your code
            break;
    
        default:
            break;
        }
    
    }
    

    This will make easier to handle many buttons click events and makes it looks simple to manage it...

    这篇关于多个按钮`OnClickListener()`机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆