单方法来实现onTouchListener()为多个按钮

问题描述 投票:7回答:8

我想要看看是否有开发出实现多个按钮的触摸监听单一方法的方式,看到我有好几个按钮,可以做几乎同样的事情。在他们做什么,唯一的区别是,他们会通过我的sendMessage()方法,并且按钮需要保持下来要发送的消息多久发送消息。如果有办法做到这一点,可能那是什么呢?而且,为什么不这样的工作?

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

电话 -

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()中有一个登录呼叫利用布尔值,我想看看它被设置当它传递的。这就是它的按键的释放过程中被调用的唯一原因。

android button ontouchlistener
8个回答
19
投票

是的,你说得对,有一个更好的办法。单个TouchListener,处理一切,确定它是通过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,然后你的代码会更简单。

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视图中设置工作。


5
投票

随着ButterKnife它会是这样。 (在我的情况为ImageView的按钮)

@OnTouch({R.id.Button1, R.id.Button2, R.id.Button3})
public boolean buttonsTouched(ImageView button, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            --(Your ACTION on Pressed)--
            return true;
        case MotionEvent.ACTION_UP:
            --(Your ACTION on Release)--
            return true;
    }
    return true;
}

4
投票

是啊,有做同样的更好的方法。 1)让你的类实现OnTouchListener。 2)本侦听器添加到每个按钮应该处理触摸事件。像这样:

button1.setOnTouchListener(this);

3),并在此public boolean onTouch(View arg0, MotionEvent arg1) {});

方法,你可以在被触摸的视图中使用开关的情况下。第一个参数,即arg0是触摸事件已经被分派给图。在你的情况下,将不同的按钮。事情是这样的:

public boolean onTouch(View arg0, MotionEvent arg1) {
    if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
        switch (arg0.getId()) {
        case R.id.button1: // Id of the button
            // Your code goes here
            break;

        case R.id.button2: // Id of the button
            // Your code goes here
            break;

        default:
            break;
        }
    }
    return true;
}

1
投票
OnTouchListener mOnTouchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Code goes here
    return true;
    }
};

button1.setOnTouchListener(mOnTouchListener);
button2.setOnTouchListener(mOnTouchListener);

1
投票

我合并两个答案,这是我的代码

public class MyActivity extends Activity implements OnTouchListener {
Button mGreen, mRed;

void initialization() {

    ... //do initialization stuff

    mGreen.setOnTouchListener(this);
    mRed.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {


    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            actionDown();
            break;
        case MotionEvent.ACTION_UP:
            actionUp();
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            break;

        case MotionEvent.ACTION_POINTER_UP:
            break;

        case MotionEvent.ACTION_MOVE:
            actionMove();
            break;
    }

    return true;
}

public void actionDown() {
    switch (view.getId()) {
        case R.id.button_green:
            //todo
            break;

        case R.id.button_red:
            //todo
            break;
    }
}

public void actionUp() {
    switch (view.getId()) {
        case R.id.button_green:
            //todo
            break;


        case R.id.button_red:
            //todo
            break;
    }
}

public void actionMove() {
    switch (view.getId()) {
        case R.id.button_green:
            // todo
            break;

        case R.id.button_red:
            // todo
            break;
    }

}}

我希望这个代码将会帮助别人


0
投票

你为什么不使用butterknife?

@Nullable @OnClick({R.id.btn1, R.id.btn2,R.id.btn3, R.id.btn4})
public void doStuff(Button button) {}

0
投票
@Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
            {
                case MotionEvent.ACTION_DOWN:
                    shouldClick = true;
                    .
                    .
                    break;
                case MotionEvent.ACTION_UP:
                    if (shouldClick)
                        view.performClick();
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    //Do your stuff
                    shouldClick = false;
                    break;
            }
            rootLayout.invalidate();
            return true;
        }

0
投票

在我的情况我需要的是类似于上述的答案,但我的目的是不同的...

我希望当你触摸屏幕,无论UP隐藏我的键盘,或DOWN

   /**
     * Hide keyboard on touching the screen
     * <a href="https://stackoverflow.com/questions/17864143/single-method-to-implement-ontouchlistener-for-multiple-buttons">how to hide on touch</a>
     */
    @OnTouch(R.id.scroll_sign_up_step2_view)
    public boolean hideKeyBoardOnTouch() {
        hideKeyboard();
        return true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.