如何连续运行LongClick事件?

时间:2012-06-07 06:06:48

标签: android button onclick forward

是否可以使用onLongClick按钮事件实现快进按钮?

修改

  

我在onlongclicklistner中使用了runnable并添加了需要的参考代码:)

   Button.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {


            final Runnable r = new Runnable()
            {
                public void run() 
                {//do the forwarding logic here


                     if(Button.isPressed()){

                            Button.postDelayed(this, 1000); //delayed for 1 sec
                        }else{

                        Button.postInvalidate();
                        Button.invalidate();
                        }
                }
            };

            Button.post(r);

            return true;
            }
    });

2 个答案:

答案 0 :(得分:2)

  1. onLongClick事件中,将成员变量(例如:mShouldFastForward)设置为true

  2. 在其余代码中(可能每个框架都播放了吗?)检查mShouldFastForward == true;如果是,请在该帧上执行快进。

  3. 使用onTouch事件捕获MotionEvent.ACTION_UP以将mShouldFastForward设置为false

答案 1 :(得分:0)

我已经在这个项目中完成了(项目没有完成(即抛光)但是快进工程):

https://bitbucket.org/owentech/epileptic-gibbon-android

看一下playerfragment.java:

我通过使用Threads快进媒体播放器来处理这个问题。

来自项目的示例代码:

/*******************************/
/* Fast-Forward button actions */
/*******************************/

ffbutton.setOnTouchListener(new View.OnTouchListener() {

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

   switch (event.getAction() ) { 

       case MotionEvent.ACTION_DOWN: 

           arrays.fastforwardpressed = true;
           FastForwardThread newFFThread = new FastForwardThread();
           arrays.fastforwardfrom = mp.getCurrentPosition();
           arrays.fastforwardto = arrays.fastforwardfrom;

           newFFThread.start();
           break;

       case MotionEvent.ACTION_UP:

           arrays.fastforwardpressed = false;
           mp.seekTo(arrays.fastforwardto);
           break; 

    }

    return true;
  }
});


public class FastForwardThread extends Thread
{
    public FastForwardThread()
    {
        super("FastForwardThread");
    }

    public void run()
    {
        while (arrays.fastforwardpressed == true)
        {

            arrays.fastforwardto = arrays.fastforwardto + 10000;
            int fastforwardseconds = arrays.fastforwardto / 1000;
            int hours = fastforwardseconds / 3600, remainder = fastforwardseconds % 3600, minutes = remainder / 60, seconds = remainder % 60;

            String Hours = Integer.toString(hours);
            String Minutes = Integer.toString(minutes);
            String Seconds = Integer.toString(seconds);

            if (Hours.length() == 1)
            {
                Hours = "0" + Hours;
            }

            if (Minutes.length() == 1)
            {
                Minutes = "0" + Minutes;
            }

            if (Seconds.length() == 1)
            {
                Seconds = "0" + Seconds;
            }

            arrays.formattedfftime = Hours + ":" + Minutes + ":" + Seconds;

            fastforwardHandler.sendEmptyMessage(0);

            try
            {
                sleep(100);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    }