Com*_*are 17
AFAIK,没有.它通过getLongPressTimeout()on 在框架中硬连线ViewConfiguration.
欢迎您处理自己的触摸事件并定义自己的"长按"概念.请确保它与用户期望的不同,并且很可能用户会期望所有其他应用程序使用的内容,即标准的500毫秒持续时间.
小智 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;
}
});
Run Code Online (Sandbox Code Playgroud)
这是我找到的针对此限制的最简单的工作解决方案:
//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;
}
});
Run Code Online (Sandbox Code Playgroud)
我在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
}
})
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
button.setOnVeryLongClickListener{
// Do something here
}
Run Code Online (Sandbox Code Playgroud)
这就是我使用的。它与 Cumulo 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;
}
Run Code Online (Sandbox Code Playgroud)
如果视图通常不会点击你需要调用view.setClickable(true)的view.isPressed()检查工作。
| 归档时间: |
|
| 查看次数: |
13371 次 |
| 最近记录: |