我的情况是这样的。
如果用户按下录制按钮超过1秒钟,则开始录音,否则将其视为意外触摸并且不录制。
我们尝试了基于长按和触摸列表器的解决方案,代码如下。但是我们无法以正确的顺序获取Toast消息。即使长时间单击也不会调用长按侦听器。
imgViewMic.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getActivity(), "Long Click", Toast.LENGTH_LONG).show();
imgViewMic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(getActivity(), "ACTION_DOWN", Toast.LENGTH_LONG).show();
} else if (event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(getActivity(), "ACTION_UP", Toast.LENGTH_LONG).show();
}
return true;
}
});
return true;
}
});
答案 0 :(得分:1)
试试这个
// Global varibles
double startTime, deltaTime;
//触摸听众
findViewById(R.id.mic).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Touch start time
startTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
// TouchEnd Time
deltaTime = (System.currentTimeMillis() - startTime);
// Difference > 1 Sec
if (deltaTime > 1000) {
Toast.makeText(getApplicationContext(), "Record " + deltaTime / 1000 + " Sec", 300).show();
} else {
Toast.makeText(getApplicationContext(), "Dont Record " + deltaTime / 1000 + " Sec", 300).show();
}
break;
default:
break;
}
return true;
}
});