我正在制作一个应用程序,它有一个按钮来执行操作,但我想在用户长按按钮时执行该操作。因为 Google 提供了长按持续时间 appx .5 秒,但我想自定义这个持续时间。请帮忙...
你可以试试Touch Listener
去做这个。
Try:
Handler handler = new Handler();
b.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.postDelayed(run, 5000/* OR the amount of time you want */);
break;
case MotionEvent.ACTION_CANCEL:
handler.removeCallbacks(run);
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacks(run);
break;
}
return true;
}
});
Where b
is the view
您想要长按的位置。
And Runnable
run
如下
Runnable run = new Runnable() {
@Override
public void run() {
// Your code to run on long click
}
};
希望能帮助到你... :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)