我正在做一个项目,我希望在单点触摸时显示特定的消息,并使用android在双触摸时显示另一条消息。我可以实现它。
我的示例代码位于
之下if(firstTap){
thisTime = SystemClock.u`enter code here`ptimeMillis();
firstTap = false;
}else{
prevTime = thisTime;
thisTime = SystemClock.uptimeMillis();
//Check that thisTime is greater than prevTime
//just incase system clock reset to zero
if(thisTime > prevTime){
//Check if times are within our max delay
if((thisTime - prevTime) <= DOUBLE_CLICK_MAX_DELAY){
//We have detected a double tap!
Toast.makeText(AddLocation.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
//PUT YOUR LOGIC HERE!!!!
}else{
//Otherwise Reset firstTap
firstTap = true;
}
}else{
firstTap = true;
}
}
return false;
答案 0 :(得分:9)
为什么不在推荐用户界面时使用长按事件。 Read Answer Here,我强烈建议您使用它。
或者无论如何你要实现它有两个选项,一个是this using boolean 第二个是使用Gesture Listener。
答案 1 :(得分:9)
尝试使用GestureDetector
。
public class MyView extends View {
GestureDetector gestureDetector;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// creating new gesture detector
gestureDetector = new GestureDetector(context, new GestureListener());
}
// skipping measure calculation and drawing
// delegate the event to the gesture detector
@Override
public boolean onTouchEvent(MotionEvent e) {
//Single Tap
return gestureDetector.onTouchEvent(e);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
return true;
}
}
答案 2 :(得分:3)
您可以使用长按而不是使用双击来覆盖此方法
abstract boolean onLongClick(View v)
单击并按住视图时调用。
答案 3 :(得分:3)
这是举行,但是,对于新读者,我创建了一个小型库来简化这类内容,请查看这篇文章:Double click listener on android。
图书馆非常小,这里是GitHub Repository Link。
你会像这样使用它:
Button btn = new Button(this);
btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
@Override
public void onSingleClick(View view) {
// Single tap here.
}
@Override
public void onDoubleClick(View view) {
// Double tap here.
}
});
答案 4 :(得分:1)
尝试以下修改过的代码::
//Check that thisTime is greater than prevTime
//just incase system clock reset to zero
static prevtime = 0;
thistime = Calendar.getInstance().getTimeInMillis();
if(prevTime<thistime){
//Check if times are within our max delay
if((thisTime - prevTime) <= 1000){//1 SEC
//We have detected a double tap!
Toast.makeText(AddLocation.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
prevtime=thistime;
//PUT YOUR LOGIC HERE!!!!
}else{
//Otherwise Reset firstTap
firstTap = true;
}
}else{
firstTap = true;
}
}
答案 5 :(得分:0)
我已经实现了在点击屏幕两次背景颜色和文本颜色发生变化的代码(双击)这里是代码......
int i = 0;
reader.setOnClickListener(new View.OnClickListener() {
int i = 0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
i = 0;
}
};
if (i == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (i == 2) {
if(color==1) {
reader.setTextColor(0xFF000000);
reader.setBackgroundColor(0xFFFFFFFF);
color = 2;
}else if(color==2)
{
reader.setTextColor(0xFFFFFFFF);
reader.setBackgroundColor(0xFF000000);
color=1;
}
i = 0;
}
}
});
答案 6 :(得分:0)
在Kotlin中你可以使用RxAndroid,它应该是这样的:
yourView.clicks().buffer(500, TimeUnit.MILLISECONDS, 2).filter {
it.size >= 2
}.subscribe {
// Handle double click
}
clicks()
扩展函数,该视图创建RX可观察序列。 答案 7 :(得分:0)
我已经使用接口和线程创建了DoubleClickListener。它可以与任何视图一起使用,就像经典的ClickListener一样。这样可以为双击事件添加我的侦听器。
示例
button.setOnClickListener(new DoubleTapListener(this));
在任何活动中,都应重写此方法以执行任务
@Override
public void onDoubleClick(View v) {
// Do what you like
}
让我们看一下实现。我创建了一个简单的 DoubleTapListener,但是使用这种方法可以创建复杂的触摸 监听器(三重监听器,单,双,三重监听器全部在 一个)。
1)DoubleTapListener
public class DoubleTapListener implements View.OnClickListener{
private boolean isRunning= false;
private int resetInTime =500;
private int counter=0;
private DoubleTapCallback listener;
public DoubleTapListener(Context context)
{
listener = (DoubleTapCallback)context;
}
@Override
public void onClick(View v) {
if(isRunning)
{
if(counter==1) //<-- makes sure that the callback is triggered on double click
listener.onDoubleClick(v);
}
counter++;
if(!isRunning)
{
isRunning=true;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(resetInTime);
isRunning = false;
counter=0;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
2)DoubleTapCallback
public interface DoubleTapCallback {
public void onDoubleClick(View v);
}
3)在您的活动中实现回调并覆盖方法
public class MainActivity extends AppCompatActivity implements DoubleTapCallback{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new DoubleTapListener(this)); //<-- Set listener
}
@Override
public void onDoubleClick(View v) {
// Toast to show double click
}
}
您可以看到完整的代码实现HERE
答案 8 :(得分:0)
我做了一个简单的解决方案-
buttonTab.setOnClickListener(new View.OnClickListener() {
int count = 0;
Handler handler = new Handler();
Runnable runnable = () -> count = 0;
@Override
public void onClick(View v) {
if (!handler.hasCallbacks(runnable))
handler.postDelayed(runnable, 500);
if(count==2){
/*View is double clicked.Now code here.*/
}
}
});
答案 9 :(得分:0)
我们扩展了 OnClickListener
以便它可以轻松应用于任何视图:
/**
* Allows to set double click events on views.
*
* @param action The action to perform on click.
* @param interval The maximum interval between two clicks, in milliseconds.
*/
class DoubleClickListener(private val action: (view: View) -> Unit, private val interval: Long = 800) : View.OnClickListener {
companion object {
/** The view that was clicked previously. */
private var myPreviouslyClickedView: WeakReference<View>? = null
/**
* Check if the click was a second one or not.
* @param view The view to check for.
*
* @return True if the click was a second one.
*/
private fun isSecondClick(view: View) =
myPreviouslyClickedView?.get() == view
}
/** Execute the click. */
override fun onClick(view: View?) {
if (view != null) {
// Make sure this click is the second one
if (isSecondClick(view)) {
myPreviouslyClickedView?.clear()
action(view)
} else {
// Set the previous view to this one but remove it after few moments
myPreviouslyClickedView = WeakReference(view)
view.postDelayed({ myPreviouslyClickedView?.clear() }, interval)
}
}
}
}
属性 myPreviouslyClickedView
确保可以有多个视图使用侦听器。
第 2 部分
此外,我们通过扩展 View
使分配侦听器变得更加容易:
/**
* Set a double click listener on a view.
*
* @param action The action to perform on a double click.
*/
fun View.setOnDoubleClickListener(action: (view: View) -> Unit) =
this.setOnClickListener(DoubleClickListener(action))
或者,处理两次点击之间的自定义间隔:
/**
* Set a double click listener on a view.
*
* @param interval The maximum interval between two clicks, in milliseconds.
* @param action The action to perform on a double click.
*/
fun View.setTimedOnDoubleClickListener(interval: Long, action: (view: View) -> Unit) =
this.setOnClickListener(DoubleClickListener(action, interval))
答案 10 :(得分:0)
这里是 kotlin 中双击监听器的完整示例
class MainActivity : AppCompatActivity() {
private lateinit var gDetector : GestureDetectorCompat
private lateinit var touchListener : View.OnTouchListener
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// instantiate GestureDetectorCompat
gDetector = GestureDetectorCompat(
this,
GestureDetector.SimpleOnGestureListener()
)
// Create anonymous class extend OnTouchListener and SimpleOnGestureListener
touchListener = object : View.OnTouchListener, GestureDetector.SimpleOnGestureListener() {
override fun onTouch(view: View?, event: MotionEvent?): Boolean {
gDetector.onTouchEvent(event)
gDetector.setOnDoubleTapListener(this)
return true
}
override fun onDoubleTap(e: MotionEvent?): Boolean {
Log.i("debug", "onDoubleTap")
return true
}
}
anyView.setOnTouchListener(touchListener)
}
}