开发者

Android OnTouchListener receives 2 events on one click

开发者 https://www.devze.com 2023-02-18 18:06 出处:网络
Hello I have a couple of EditText widgets that I want to change the behaviour of depending on user preferences. The user should be able to change the EditText views so a popup comes forth with a numbe

Hello I have a couple of EditText widgets that I want to change the behaviour of depending on user preferences. The user should be able to change the EditText views so a popup comes forth with a number of alternatives. When the user want to use a list for input I will set an onTouchListener instead.

It is working quite well except that the onTouchListener receives two events when I click once so two popupdialogs appear. Th开发者_StackOverflow中文版is is the OnTouchListener:

 private OnTouchListener mInputListOnTouckListener = new View.OnTouchListener()
 {
        EditText et;
        @Override
        public boolean onTouch(View ve, MotionEvent me)
        {   
            final CharSequence[] items = {"1", "2", "3"};
            et = (EditText)ve;
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Pick a number");

            builder.setItems(items, new DialogInterface.OnClickListener()
            {   
                public void onClick( DialogInterface dialogInterface, int item ) 
                {
                    et.setText(items[item]);
                    return;
                }
            });

            builder.create().show();
            return true;
        }
 };

PS: Yes I want to return true because it prevents the keyboard from showing up.


You're probably getting an ACTION_DOWN event when you touch the screen, and an ACTION_UP event when you release it. Use the getAction() method on the MotionEvent to determine which event you're getting, and do your stuff only on ACTION_UP for example.

0

精彩评论

暂无评论...
验证码 换一张
取 消