OnTap监听器实现

时间:2011-07-16 08:13:41

标签: android tap

我怀疑在特定按钮或图像视图或视图上实现了点击侦听器?因为我浏览的网站只显示整个布局,我希望我的动作可以在点击视图时执行。请帮忙。谢谢。

4 个答案:

答案 0 :(得分:9)

可以使用onClickListener()设置任何视图,onCreate()是视图类的一部分。最简单的方法是在ImageView iv = (ImageView) findViewByID(R.id.example); iv.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do what you need to do on click .... } }); 方法中设置对视图的引用。以下是图像视图的示例:

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class DoubleTapActivity extends Activity {
    //Set the double tap delay in milliseconds
    protected static final long DOUBLE_CLICK_MAX_DELAY = 1000L;
    private ImageView iView;
    private long thisTime = 0;
    private long prevTime = 0;
    private boolean firstTap = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        iView = (ImageView)findViewById(R.id.iView);
        iView.setOnTouchListener( new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(firstTap){
                    thisTime = SystemClock.uptimeMillis();
                    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(DoubleTapActivity.this, "DOUBLE TAP DETECTED!!!", Toast.LENGTH_LONG).show();
                            //PUT YOUR LOGIC HERE!!!!

                        }
                        else 
                        {
                            //Otherwise Reset firstTap
                            firstTap = true;
                        }
                    }
                    else 
                    {
                        firstTap = true;
                    }
                }
                return false;
            }
        });
    }
}

更新:DOUBLE TAP

以下是在图像视图上实现基本双击检测的示例活动:

{{1}}

答案 1 :(得分:2)

主要是为了感知点击事件,您必须在“活动”中实施GestureDetector.OnGestureListener。您可以使用onSingleTapUp(MotionEvent e)的{​​{1}}方法执行操作。

通常对于Button控件,我们使用

GestureDetector.OnGestureListener

或其他clickListners和tap事件用于ImageView或任何其他自定义视图。

答案 2 :(得分:1)

在您要进行点击的布局中添加MAX_TAP_COUNT。通过更改private long thisTime = 0; private long prevTime = 0; private int tapCount = 0; private static final int MAX_TAP_COUNT = 5; protected static final long DOUBLE_CLICK_MAX_DELAY = 500; public void tapEvent(View v){ if (SystemClock.uptimeMillis() > thisTime) { if ((SystemClock.uptimeMillis() - thisTime) > DOUBLE_CLICK_MAX_DELAY * MAX_TAP_COUNT) { Log.d(TAG, "touch event " + "resetting tapCount = 0"); tapCount = 0; } if (tapCount()) { //DO YOUR LOGIC HERE } } } private Boolean tapCount(){ if (tapCount == 0) { thisTime = SystemClock.uptimeMillis(); tapCount++; } else if (tapCount < (MAX_TAP_COUNT-1)) { tapCount++; } else { prevTime = thisTime; thisTime = SystemClock.uptimeMillis(); //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 * MAX_TAP_COUNT) { //We have detected a multiple continuous tap! //Once receive multiple tap, reset tap count to zero for consider next tap as new start tapCount = 0; return true; } else { //Otherwise Reset tapCount tapCount = 0; } } else { tapCount = 0; } } return false; } 值,您可以使用任意数量的点按。

TO_SECONDS(DATE_FORMAT( NOW( ) , '%Y-%m-%d %h:%i:%s' ) )

答案 3 :(得分:0)

@Override
public boolean onTouchEvent(MotionEvent event) {

    // on action down do your work...
    return super.onTouchEvent(event);
}