当按钮处于按下状态时,连续调用方法

时间:2014-03-24 06:23:28

标签: android button onclick

只要按下按钮,我就会尝试连续调用方法。

public void onClick(View view) {
method();
}



<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="onClick" />

7 个答案:

答案 0 :(得分:1)

您可以在要执行的method()中编写while循环。收到ACTION_UP时,while会退出 这样的事情会起作用:

boolean pressed = true;  
private void method() {  
    while(pressed) {  
        //Code  
    }  
}

当你得到ACTION_UP时,将布尔值设为false。

答案 1 :(得分:0)

您必须实现onTouchListener并使用switch case覆盖onTouch()方法。

float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

      return true;
    case MotionEvent.ACTION_MOVE:

      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

答案 2 :(得分:0)

use可以使用OnLongClickListener()

答案 3 :(得分:0)

  

嗨使用onTouchListener函数,只要按下按钮就会有助于继续进行

Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP){

        // Do what you want
        return true;
    }
    return false;
}

});

答案 4 :(得分:0)

    Try this one..

            setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    maintainView = v;
                    hidingevent.onTouchEvent(event);
                    if (ViewerActivity.sClick == 1) {
                        mScaleDetector.onTouchEvent(event);
                        switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:

                            call your method();

                        case MotionEvent.ACTION_MOVE:


                        case MotionEvent.ACTION_UP:
call your method();

                        case MotionEvent.ACTION_POINTER_UP:


                                       int index = event.getActionIndex();
                        int action = event.getActionMasked();
                        int pointerId = event.getPointerId(index);
                        switch (action) {
                        case MotionEvent.ACTION_DOWN:
call your method();
                        case MotionEvent.ACTION_MOVE:

                        case MotionEvent.ACTION_UP:
call your method();

                        case MotionEvent.ACTION_CANCEL:


                        }
                        return true;
                    }

                }

            });

答案 5 :(得分:0)

试试这个:

class OTL extends Handler implements OnTouchListener {

    private int cnt;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            sendEmptyMessage(0);
            Log.d(TAG, "onTouch ACTION_DOWN ");
        } else
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_UP) {
            removeMessages(0);
        }
        return false;
    }

    @Override
    public void handleMessage(Message msg) {
        Log.d(TAG, "run your method here " + cnt++);
        sendEmptyMessage(0);
    }
}

测试代码:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
ll.addView(button);
button.setOnTouchListener(new OTL());
setContentView(ll);

答案 6 :(得分:0)

这里是按住监听器执行任务的位置。 添加一些延迟以使触摸事件超排队

  @SuppressLint("HandlerLeak")
        class FrontTouchHandler extends Handler implements View.OnTouchListener {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_DOWN) {
                    sendEmptyMessage(0);
                } else if (action == MotionEvent.ACTION_UP) {
                    removeMessages(0);
                }
                return false;
            }

            @Override
            public void handleMessage(Message msg) {
                    //perform task here 
                    sendEmptyMessageDelayed(0, 125);
                }
            }
        }