Unwilling EditText onTouchListener Function Call [

2019-02-24 01:15发布

Possible Duplicate:
public boolean onKey() called twice?

I have an EditText field which calls a popUp view with radio buttons. PopUp and RadioGroup implementation works nice. But I just realize when pressed or Touch to EditText, onTouchListener is called 2 times. I also just realize that the reason of my previous question is the same issue. Here is the the EditText;

etOdemeSekli = (EditText)findViewById(R.id.etOdemeSekli);
        etOdemeSekli.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                inflatePopUpOdemeSekli();
                Log.d("****","Inflate");                    
            return false;
            }
        }); 

and here is the xml for EditText

<EditText
    android:layout_weight="1"                   
    android:id="@+id/etOdemeSekli"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/odemeSekliHint"
    android:focusableInTouchMode="false">
</EditText>

Because of this double call, popup acts weird. The dismiss() call does not function properly. What could be the reason? It is really really annoying, thank you.

1条回答
等我变得足够好
2楼-- · 2019-02-24 01:41

The double call is because the touch listener fires twice (at least!), once for when the finger lands on the EditText (ACTION_DOWN) and once when you lift the finger (ACTION_UP). To fix it, just make sure you only activate on one case. Alternatively, you could just set an onClick listener instead.

      public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
              inflatePopUpOdemeSekli();
            }

            return false;
      }
查看更多
登录 后发表回答