单一的方法来实现onTouchListener()的多个按钮 [英] Single method to implement onTouchListener() for multiple buttons

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

问题描述

我想看看是否有一种方式来创建实现触摸监听多个按钮的一个方法,看到我有好几个按钮,做几乎同样的事情。在他们做什么的唯一的区别是,它们将发送通过我sendMessage()方法,和按钮需要被按住要发送的消息的时间长度的信息。如果有办法做到这一点,有什么会这样呢?而且,为什么不这样的工作?

I am trying to see if there is a way to create a single method to implement a touch listener for multiple buttons, seeing as I have quite a few buttons that do almost exactly the same thing. The only difference in what they do is the message that they would send through my sendMessage() method, and how long the button needs to be held down for the message to be sent. If there is a way to do it, what might that be? And also, why wouldn't something like this work?

//Within onCreate Method...
Button mButton = (Button) findViewbyId(R.id.three_sec_button);
mButton = addTouchTimer(mButton, 3, 3);

电话 -

Calls -

private Button addTouchTimer(Button button, final int sec, final int messageNum){
        button.setOnTouchListener(new View.OnTouchListener() {
            boolean longEnough = false;
            long realTimeLeft = sec * 1000;
            @Override
            // This will make it so that a message is only sent if the button is held down for 3 seconds
            // Otherwise it won't send. It is sent during the hold down process, releasing it returns a false
            // value and no message is sent.
            public boolean onTouch(View arg0, MotionEvent arg1) {
                Log.d("Button", "Touchy Touchy!");
                if(arg1.getAction() == MotionEvent.ACTION_DOWN){
                    buttonPressTime = new CountDownTimer(realTimeLeft, 1000){
                        @Override
                        public void onTick(long millisUntilDone){
                                realTimeLeft = millisUntilDone;
                        }

                        @Override
                        public void onFinish() {  
                            long timeLeft = realTimeLeft;
                            long currTime = System.currentTimeMillis();
                            long realFinishTime = currTime + timeLeft;
                                while(currTime < realFinishTime){
                                    currTime = System.currentTimeMillis();
                                }
                            longEnough = true;
                            sendEmergencyMessage(longEnough, messageNum);
                        }
                    }.start();
                }
                else if(arg1.getAction() == MotionEvent.ACTION_UP){
                    buttonPressTime.cancel();
                    sendMessage(longEnough, messageNum);
                }
                return longEnough;
            }           
        });

        return button;
    }

这似乎只是出于效率的缘故,必须有这样做比实现个人监听每个按钮的更好的方法。作为一个说明,那么SendMessage()中有一个通话记录,利用布尔值,我想看看它被设置为当它传递。这是它的按钮的释放期间调用的唯一原因。

It just seems that for efficiency's sake there has to be a better way of doing it than implementing individual listeners for each button. As a note, sendMessage() has a Log call in it that utilizes the boolean value, I want to see what it is set as when it is passed. That is the only reason it is called during the release of the button.

推荐答案

是的,你说得对,有一个更好的办法。处理一切,确定一个单一的TouchListener它是通过ID哪个按钮。

Yes you are right, there is a better way. A single TouchListener that handles everything, determining which button it is via the id.

void intialization(){
     Button m1, m2, m3, m4;
     ... //do initialization stuff
     m1.setId(1);
     m2.setId(2);
     m3.setId(3);
     m4.setId(4);
     MyTouchListener touchListener = new MyTouchListener();
     m1.setOnTouchListener(touchListener);
     m2.setOnTouchListener(touchListener);
     m3.setOnTouchListener(touchListener);
     m4.setOnTouchListener(touchListener);
 }

public class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(v.getId()){
            case 1:
                //do stuff for button 1
                break;
            case 2:
                //do stuff for button 2
                break;
            case 3:
                //do stuff for button 3
                break;
            case 4:
                //do stuff for button 4
                break;
        }
        return true;
    }

}

这就是你如何做到这一点!为ID的数值方法是在这种情况下非常有用。另一种方法是让你的活动实施OnTouchListener在你的活动,然后你的code会更简单。

And that's how you'd do it! A numerical approach for the id's is very helpful in this case. Another approach is to have your activity implement the OnTouchListener in your activity, and then your code would be even simpler.

public class MyActivity extends Activity implements OnTouchListener {

    void initialization(){
        Button m1, m2, m3, m4;
        ... //do initialization stuff
        m1.setId(1);
        m2.setId(2);
        m3.setId(3);
        m4.setId(4);
        m1.setOnTouchListener(this);
        m2.setOnTouchListener(this);
        m3.setOnTouchListener(this);
        m4.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(v.getId()){
            case 1:
                //do stuff for button 1
                break;
            case 2:
                //do stuff for button 2
                break;
            case 3:
                //do stuff for button 3
                break;
            case 4:
                //do stuff for button 4
                break;
        }
        return true;
    }

}

注:此方法也将也适用于OnClickListener,OnCheckedChangeListener,或任何其他的侦听器,你会在Android视图设置

Note: This approach also will also work for OnClickListener, OnCheckedChangeListener, or any other listener that you would set on an Android view.

这篇关于单一的方法来实现onTouchListener()的多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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