【发布时间】:2012-03-31 18:14:57
【问题描述】:
我正在通过 setOnLongClickListener() 监听视图的长按事件。我可以更改长点击延迟/持续时间吗?
【问题讨论】:
我正在通过 setOnLongClickListener() 监听视图的长按事件。我可以更改长点击延迟/持续时间吗?
【问题讨论】:
这是我设置持续时间长按的方式
private int longClickDuration = 3000;
private boolean isLongPress = false;
numEquipeCheat.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isLongPress = true;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (isLongPress) {
Vibrator vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(100);
// set your code here
// Don't forgot to add <uses-permission android:name="android.permission.VIBRATE" /> to vibrate.
}
}
}, longClickDuration);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
isLongPress = false;
}
return true;
}
});
【讨论】:
AFAIK,不。它通过getLongPressTimeout()ViewConfiguration 在框架中硬连线。
欢迎您处理自己的触摸事件并定义自己的“长按”概念。只需确保它与用户的期望没有太大的不同,并且用户很可能会期望所有其他应用使用什么,即标准的 500 毫秒持续时间。
【讨论】:
我在 Kotlin 中定义了一个扩展函数,灵感来自@Galoway 答案:
fun View.setOnVeryLongClickListener(listener: () -> Unit) {
setOnTouchListener(object : View.OnTouchListener {
private val longClickDuration = 2000L
private val handler = Handler()
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN) {
handler.postDelayed({ listener.invoke() }, longClickDuration)
} else if (event?.action == MotionEvent.ACTION_UP) {
handler.removeCallbacksAndMessages(null)
}
return true
}
})
}
像这样使用它:
button.setOnVeryLongClickListener {
// Do something here
}
【讨论】:
这是我使用的。它类似于 Cumolo Nimbus 的回答,但有两个显着差异。
view.isPressed 以确保用户在触摸事件期间没有离开视图。这模仿了 onClick 和 onLongClick 的默认系统行为。long longPressTimeout = 2000;
@Override
public boolean onTouch(View view, MotionEvent event) {
if (view.isPressed() && event.getAction() == MotionEvent.ACTION_UP) {
long eventDuration = event.getEventTime() - event.getDownTime();
if (eventDuration > longPressTimeout) {
onLongClick(view);
} else {
onClick(view);
}
}
return false;
}
如果视图不能正常点击,您需要调用view.setClickable(true) 以使view.isPressed() 检查生效。
【讨论】:
这是我发现的最简单的解决这个限制的方法:
//Define these variables at the beginning of your Activity or Fragment:
private long then;
private int longClickDuration = 5000; //for long click to trigger after 5 seconds
...
//This can be a Button, TextView, LinearLayout, etc. if desired
ImageView imageView = (ImageView) findViewById(R.id.desired_longclick_view);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
then = (long) System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if ((System.currentTimeMillis() - then) > longClickDuration) {
/* Implement long click behavior here */
System.out.println("Long Click has happened!");
return false;
} else {
/* Implement short click behavior here or do nothing */
System.out.println("Short Click has happened...");
return false;
}
}
return true;
}
});
【讨论】:
这就是我在处理 onclick 和自定义长按同一个按钮时所做的
public static final int LONG_PRESS_DELAY_MILLIS = 3000;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
saveInfo();
break;
default:
break;
}
}
@Override
public boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
initSendInfo(v, System.currentTimeMillis());
return true;
default:
return false;
}
}
private void initSendInfo(final View v, final long startTime) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (v.isPressed() && System.currentTimeMillis() - startTime >= LONG_PRESS_DELAY_MILLIS) {
sendInfo();
return;
} else if (!v.isPressed()) {
return;
}
}
}, LONG_PRESS_DELAY_MILLIS);
}
【讨论】:
这对我在API 29 有效,同时使用 Kotlin 保留按钮的视觉向下状态:
fun AppCompatButton.setOnVeryLongClickListener(
holdDuration: Long = 1000L,
listener: () -> Unit
) {
val handler = Handler()
val originalText = this.text
setOnTouchListener { v, event ->
if (event?.action == MotionEvent.ACTION_DOWN) {
// update the copy of the button
text = "$originalText (KEEP HOLDING)"
// fire the listener after our hold duration
handler.postDelayed({ listener.invoke() }, holdDuration)
} else if(event?.action == MotionEvent.ACTION_UP) {
// update the copy of the button
text = "$originalText (TAP & HOLD)"
// in case of an interruption, cancel everything
handler.removeCallbacksAndMessages(null)
}
// return false in order to retain the button's visual down state
false
}
}
button.setOnVeryLongClickListener {
println("very long!")
}
【讨论】: