如果按下5秒钟,按钮将启动一个新活动

时间:2015-03-24 16:29:21

标签: android eclipse

如果按下按钮5秒钟,我想开始一项新活动。我在Touch Listener上使用。

我使用的代码如下:

Button btn = (Button) findViewById(R.id.button1);
        btn.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getActionMasked();

                if (action == MotionEvent.ACTION_DOWN) {

                } else if (action == MotionEvent.ACTION_UP
                        || action == MotionEvent.ACTION_CANCEL) {

                }
                // TODO Auto-generated method stub
                return false;
            }
        });

我如何在这之间使用处理程序,谢谢你

4 个答案:

答案 0 :(得分:1)

试试这个

您可以使用Touch Listener执行此操作。

尝试:

Handler handel = new Handler();     b.setOnTouchListener(new View.OnTouchListener(){

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN:
            handel.postDelayed(run, 5000/* OR the amount of time you want */);
            break;

        default:
            handel.removeCallbacks(run);
            break;

        }
        return true;
    }
});

其中b是您要长按的视图(在您的情况下应该是按钮)。

Runnable运行如下

Runnable run = new Runnable() {

    @Override
    public void run() {
        // Your code to run on long click
Intent i=new Intent(this, NewActivity.class);
startActivity(i);

    }
};

答案 1 :(得分:0)

这应该会让你到那里。安排任务,然后检查按钮是否仍然按下。 Android Touch Event determining duration

答案 2 :(得分:0)

你应该这样做 -

//class member
private long firstTime=0,secondTime=0;

现在在方法

Button btn = (Button) findViewById(R.id.button1);
        btn.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getActionMasked();

                if (action == MotionEvent.ACTION_DOWN) {
                      firstTime = System.currentTimeMillis();

                } else if (action == MotionEvent.ACTION_UP
                        || action == MotionEvent.ACTION_CANCEL) {
                      secondTime = System.currentTimeMillis();
                      if(secondTime-firstTime>=5000){ // at least 5000 ms touch down time
                          // launch your target activity from here
                      }else{ //ignore it}
                    firstTime=0; //reseting the value for the next time
                    secondTime=0;//reseting the value for the next time

                }
                // TODO Auto-generated method stub
                return false;
            }
        });

答案 3 :(得分:0)

 @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked();

            if (action == MotionEvent.ACTION_DOWN) {
                prev=System.currentTimeMillis(); 

            } else if (action == MotionEvent.ACTION_UP
                    || action == MotionEvent.ACTION_CANCEL) {
                curr=System.currentTimeMillis();
                if(curr-prev>=5000){
                   //do your actions here,prev,curr are fields in a class
                 }
            }
            // TODO Auto-generated method stub
            return false;
        }`