区分单击并双击android中的imageView

时间:2014-01-21 15:44:52

标签: android onclick

我尝试了以下代码来区分单击和双击。单击即可。当我双击imageview时,单击和双击部分内的代码执行。

private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
    private long lastPressTime;
    boolean mHasDoubleClicked;

img_feat_orgn_item.setOnClickListener(new OnClickListener() {
 @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    findDoubleClick();
    }
    });

private boolean findDoubleClick() {
        // Get current time in nano seconds.
    long pressTime = System.currentTimeMillis();
        // If double click...
        if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
            mHasDoubleClicked = true;           
            startActivity(new Intent(
                    Wv_HomePage.this,
                    NewDonation.class));
            // double click event....
        } else { // If not double click....
            mHasDoubleClicked = false;
            Handler myHandler = new Handler() {
                public void handleMessage(Message m) {
                    boolean mHasDoubleClicked = false;
                    if (!mHasDoubleClicked) {
                        // single click event
                        feature_class_val = listData_Feature_Organization
                                .get(j);
                        Intent intent = new Intent(
                                Wv_HomePage.this,
                                OrganizationDetails.class);
                        Bundle b = new Bundle();
                        b.putString("orgn_name",
                                feature_class_val.name);
                        intent.putExtras(b);
                        startActivity(intent);

                    }
                }
            };
            Message m = new Message();
            myHandler.sendMessageDelayed(m, DOUBLE_PRESS_INTERVAL);
        }
        lastPressTime = pressTime;
        return mHasDoubleClicked;
    }

5 个答案:

答案 0 :(得分:2)

按照惯例,Android应用没有双击。

也许您宁愿使用onLongClick

虽然,如果你真的想要双击,请查看GestureDetector.OnDoubleTapListener

答案 1 :(得分:1)

只需稍加改动,我的代码就可以了。我把mHasDoubleClicked布尔放在处理程序中,这就麻烦了。以下代码有效。

img_feat_orgn_item.setOnClickListener(new OnClickListener() {
     @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        findDoubleClick();
    if (mHasDoubleClicked) {
                        Log.v("double click","double click");
    startActivity(new Intent(
                        Wv_HomePage.this,
                        NewDonation.class));
                    }
        }
        });

    private boolean findDoubleClick() {
            // Get current time in nano seconds.
        long pressTime = System.currentTimeMillis();
            // If double click...
            if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                mHasDoubleClicked = true;           

                // double click event....
            } else { // If not double click....
                mHasDoubleClicked = false;
                Handler myHandler = new Handler() {
                    public void handleMessage(Message m) {

                        if (!mHasDoubleClicked) {
                            // single click event
                            feature_class_val = listData_Feature_Organization
                                    .get(j);
                            Intent intent = new Intent(
                                    Wv_HomePage.this,
                                    OrganizationDetails.class);
                            Bundle b = new Bundle();
                            b.putString("orgn_name",
                                    feature_class_val.name);
                            intent.putExtras(b);
                            startActivity(intent);

                        }
                    }
                };
                Message m = new Message();
                myHandler.sendMessageDelayed(m, DOUBLE_PRESS_INTERVAL);
            }
            lastPressTime = pressTime;
            return mHasDoubleClicked;
        }

答案 2 :(得分:1)

这是我的代码,似乎有用。

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MultiTapImageView extends ImageView{

    private GestureDetector gestureDetector;
    private MultiTapImageViewListener mListener;

    public interface MultiTapImageViewListener {

        void onDoubleTap();
        void onSingleTap();

    }

    public MultiTapImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());

    }

    public void setDoubleTapListener(MultiTapImageViewListener listener){
        mListener = listener;
    }


    @Override
    public boolean onTouchEvent(MotionEvent e) {
        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();

            if(mListener != null){
                mListener.onDoubleTap();
            }

            return true;
        }


        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if(mListener != null){
                mListener.onSingleTap();
            }

            return true;
        }
    }
}

答案 3 :(得分:0)

试试这个。

   btn.setOnClickListener(new View.OnClickListener() {

        volatile int i = 0;

        @Override
        public void onClick(View v) {

            i++;
            Handler handler = new Handler();

            Runnable r = new Runnable() {
                @Override
                public void run() {
                    if (i == 1) {
                        //single click logic
                    }
                }
            };

            if (i == 1) {
                handler.postDelayed(r, 150);
            } else if (i == 2) {
                handler.removeCallbacks(r);
                i = 0;
                //Double click logic
            }
        }
    }
    );

或者您可以通过以下网址查看 DOUBLE-TAP 示例。 在listView中使用。我希望它对你有用。

https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master

答案 4 :(得分:0)

btn.setOnClickListener(new View.OnClickListener() {

                               volatile int i = 0;

                               @Override
                               public void onClick(View v) {

                                   i++;
                                   Handler handler = new Handler();

                                   Runnable r = new Runnable() {
                                       @Override
                                       public void run() {
                                           if (i == 1) {
                                               //single click logic

                                               i = 0; //set to zero 
                                           }
                                       }
                                   };

                                   if (i == 1) {
                                       handler.postDelayed(r, 300);
                                   } else if (i == 2) {
                                       handler.removeCallbacks(r);
                                       i = 0;
                                       //Double click logic

                                   }
                               }
                           }
    );