Unwilling EditText onTouchListener Function Call [duplicate]

纵然是瞬间 提交于 2019-12-31 00:41:25

问题


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:


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;
      }


来源:https://stackoverflow.com/questions/10377049/unwilling-edittext-ontouchlistener-function-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!