4

在安卓中,

从用户按下按钮直到他/她释放该按钮,如何重复调用函数?

我检查了 clickListener 和 longclicklistener,但他们似乎没有做我想做的事。

谢谢你。

4

3 回答 3

2

您可以使用OnTouchListener

public class MainActivity extends Activity implements OnTouchListener
{

    private Button button;

    // ...

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // ...

        button = (Button) findViewById(R.id.button_id);
        button.setOnTouchListener(this);

        // ...
    }

    // ...

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        /* get a reference to the button that is being touched */
        Button b = (Button) v;

        /* get the action of the touch event */
        int action = event.getAction();

        if(action == MotionEvent.ACTION_DOWN)
        {
            /*
                A pressed gesture has started, the motion contains
                the initial starting location.
            */
        }
        else if(action == MotionEvent.ACTION_UP)
        {
            /*
                A pressed gesture has finished, the motion contains
                the final release location as well as any intermediate
                points since the last down or move event.
            */
        }
        else if(action == MotionEvent.ACTION_MOVE)
        {
            /*
                A change has happened during a press gesture (between
                ACTION_DOWN and ACTION_UP). The motion contains the
                most recent point, as well as any intermediate points
                since the last down or move event.
            */
        }
        else if(action == MotionEvent.ACTION_CANCEL)
        {
            /*
                The current gesture has been aborted. You will not
                receive any more points in it. You should treat this
                as an up event, but not perform any action that you
                normally would.
            */
        }
    }
}
于 2012-05-07T16:33:53.173 回答
0

如果我是对的: 单击:用户按下并松开手指。触摸:用户的手指仍在设备上。

也许您应该尝试使用 onTouchListener?我从来没用过...

于 2012-05-07T16:27:35.003 回答
0

使用 OnTouchListener 调用函数,直到用户释放按钮:

private OnTouchListener otl_conn = (OnTouchListener) new TouchListenerConn();
private Button bv = null;
bv = (Button) findViewById(R.id.xxxxbutton);
bv.setOnTouchListener(otl_conn);
 class  TouchListenerConn implements OnTouchListener  
    {
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
            //call function here
            break;
            case MotionEvent.ACTION_UP:
            //DO SOMETHING
            xxxx....;
            break;
            }
            return true;
        }
    }
于 2012-05-07T16:32:40.023 回答