我正在通过setOnLongClickListener()监听View的长按事件。我可以更改长按延迟/持续时间吗?
答案 0 :(得分:17)
AFAIK,没有。它通过getLongPressTimeout()
上的ViewConfiguration
在框架中硬连接。
欢迎您处理自己的触摸事件并定义自己的“长按”概念。请确保它与用户期望的不同,并且很可能用户会期望所有其他应用程序使用的内容,即标准的500毫秒持续时间。
答案 1 :(得分:16)
这是我长按设定持续时间的方式
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;
}
});
答案 2 :(得分:4)
这是我发现这个限制的最简单的工作方案:
//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;
}
});
答案 3 :(得分:3)
这就是我使用的。它类似于Cumulo Nimbus&#39;回答,有两个值得注意的差异。
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()
以使ng-required="field.required"
支票生效。
答案 4 :(得分:2)
我在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
}
答案 5 :(得分:0)
这就是我处理同一个按钮的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(View v, 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);
}
答案 6 :(得分:0)
这是在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!")
}