ANDROID:创建一个需要按下X秒的按钮

Arc*_*are 0 java android button

我要找的是有一个按钮,其中:

如果你只是推它,什么都不做.

如果你按住它,比方说,3秒,执行一个动作().

知道该怎么办?

先感谢您.

Jav*_*ave 5

如果你想能够指定时间,这里有一个实现OnTouchListener,为你做,而不使用Timer:

class TimedTouchListener implements OnTouchListener{
    private final long millisRequired = 3000;
    private long downTime;

    @Override
    public boolean onTouch(View v, MotionEvent event){
        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            downTime = System.currentTimeMillis();
            return true;
            break;
        case MotionEvent.ACTION_UP:
            long upTime = System.currentTimeMillis();
            if( upTime - downTime > millisRequired ){
                doAction(); //doAction can be a method call, or any code you want to be executed.
                return true;
            }else{
                return true;
            }
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您不需要指定时间,请OnLongClickListener按照其他人的建议进行操作.