您可以使用相同的OnClickListener不同的按钮? [英] Can you use the same OnClickListener for different buttons?

查看:119
本文介绍了您可以使用相同的OnClickListener不同的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的,可我用的是同一个OnClickListener不同的按钮?如果是的话,我怎么哪个按钮点击从产生的?我现在有4个按钮,每个按钮都有自己的OnClickListener。每个OnClickListener都在做同样的事情,除了获取按钮的文本被点击。我想创建一个OnClickListener,但我无法弄清楚如何确定哪个按钮被点击。谢谢

 保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);

        MTTS =新TextToSpeech(这一点,新TextToSpeech.OnInitListener(){

            @覆盖
            公共无效的OnInit(INT为arg0){
                // TODO自动生成方法存根

            }
        });
        的setContentView(R.layout.home);
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        按钮按钮2 =(按钮)findViewById(R.id.button2);
        按钮按钮3 =(按钮)findViewById(R.id.button3);
        按钮将Button4 =(按钮)findViewById(R.id.button4);

        //装载第一个字
        button1.setOnClickListener(button1ClickListener);
        button2.setOnClickListener(button2ClickListener);
        button3.setOnClickListener(button3ClickListener);
        button4.setOnClickListener(button4ClickListener);

    }
 

$ C $下以粗体显示的不同部分听众

 私人OnClickListener button1ClickListener =新View.OnClickListener(){
    @覆盖
    公共无效的onClick(查看为arg0){
        ** Button按钮=(按钮)findViewById(R.id.button1)**
        handleButtonClick(按钮);
    }
};

私人OnClickListener button2ClickListener =新View.OnClickListener(){
    @覆盖
    公共无效的onClick(查看为arg0){
        ** Button按钮=(按钮)findViewById(R.id.button2)**
        handleButtonClick(按钮);

    }
};

私人OnClickListener button3ClickListener =新View.OnClickListener(){
    @覆盖
    公共无效的onClick(查看为arg0){
        ** Button按钮=(按钮)findViewById(R.id.button3)**
        handleButtonClick(按钮);

    }
};

私人OnClickListener button4ClickListener =新View.OnClickListener(){
    @覆盖
    公共无效的onClick(查看为arg0){
        ** Button按钮=(按钮)findViewById(R.id.button4)**
        handleButtonClick(按钮);

    }
};
 

$ C $下handleButtonclick

 私人无效handleButtonClick(Button按钮){
        如果(button.getText()。等于(currentWord)){
            currentScore + = availableScore;
            TextView的得分=(TextView中)findViewById(R.id.textViewScore);
            score.setText(将String.valueOf(分));
            CURRENTINDEX ++;
            availableScore = 4;
            InitializeGame();
        }
        其他{
            availableScore--;
            button.setEnabled(假);
        }
    }
 

从KARTHIK建议我修改了code以下内容:

 保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);

        MTTS =新TextToSpeech(这一点,新TextToSpeech.OnInitListener(){

            @覆盖
            公共无效的OnInit(INT为arg0){
                // TODO自动生成方法存根

            }
        });
        的setContentView(R.layout.home);
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        按钮按钮2 =(按钮)findViewById(R.id.button2);
        按钮按钮3 =(按钮)findViewById(R.id.button3);
        按钮将Button4 =(按钮)findViewById(R.id.button4);

        //装载第一个字
        button1.setOnClickListener(buttonClickListener);
        button2.setOnClickListener(buttonClickListener);
        button3.setOnClickListener(buttonClickListener);
        button4.setOnClickListener(buttonClickListener);

    }
 

OnClickListener code

 私人OnClickListener buttonClickListener =新View.OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){
            handleButtonClick((按钮)为arg0);
        }
    };
 

解决方案

是的,它是可能的。我写了下面这样的例子应该是比较简单的。

像往常一样,添加了OnClickListener所有按钮如下:

  btn1.setOnClickListener(本);
btn2.setOnClickListener(本);
btn3.setOnClickListener(本);
 

然后添加的onClick()事件,如下所示:

  @覆盖
公共无效的onClick(视图v){
    // TODO自动生成方法存根
    如果(V == BTN1){
        //要做的事
    }
    如果(V == BTN2){
        //要做的事
    }
    如果(V == btn3){
        //要做的事
    }
}
 

这应该工作得很好。不要忘了implent的View.OnClickListener为您的类中,你的onCreate是上面设置的OnClickListener将是不正确present否则所有这些语句。

In android, can I use the same OnClickListener for different buttons? If yes, how do I get which button the click is generated from? I currently have 4 buttons and each button have their own OnClickListener. Each of the OnClickListener are doing the same exact thing with the exception of getting the text of the button being clicked. I want to create a single OnClickListener but I can't figure out how to determine which button is being clicked. Thank you

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        setContentView(R.layout.home);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);

        //Load First Word
        button1.setOnClickListener(button1ClickListener);
        button2.setOnClickListener(button2ClickListener);
        button3.setOnClickListener(button3ClickListener);
        button4.setOnClickListener(button4ClickListener);

    }

Code for the listener with the the different part in bold

private OnClickListener button1ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button1);**
        handleButtonClick(button);
    }
};

private OnClickListener button2ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button2);**
        handleButtonClick(button);

    }
};

private OnClickListener button3ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button3);**
        handleButtonClick(button);

    }
};

private OnClickListener button4ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        **Button button = (Button)findViewById(R.id.button4);**
        handleButtonClick(button);

    }
};

Code for handleButtonclick

private void handleButtonClick(Button button) {
        if(button.getText().equals(currentWord)){
            currentScore += availableScore;
            TextView score = (TextView)findViewById(R.id.textViewScore);
            score.setText(String.valueOf(score));
            currentIndex++;
            availableScore = 4;
            InitializeGame();
        }
        else{
            availableScore--;
            button.setEnabled(false);
        }
    }

With recommendation from Karthik I modified my code to the following:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        setContentView(R.layout.home);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);

        //Load First Word
        button1.setOnClickListener(buttonClickListener);
        button2.setOnClickListener(buttonClickListener);
        button3.setOnClickListener(buttonClickListener);
        button4.setOnClickListener(buttonClickListener);

    }

OnClickListenerCode

private OnClickListener buttonClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            handleButtonClick((Button)arg0);
        }
    };

解决方案

Yes it is possible. I have written an example below that should be relatively straight forward.

As usual, add the OnClickListener for all the buttons as follows:

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

then add the onClick() event as shown below:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == btn1){
        //Things to do  
    }
    if(v == btn2){
        //Things to do      
    }
    if(v == btn3){
        //Things to do  
    } 
}

This should work just fine. Don't forget to implent the View.OnClickListener for your class in which your onCreate is present otherwise all those statements that set the OnClickListener above will be incorrect.

这篇关于您可以使用相同的OnClickListener不同的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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