简体   繁体   English

需要双击 EditText 才能让点击监听器响应

[英]Needing to double click EditText for click listener to respond

I have a section of code where I want to change the text showing in a textView when the user selects an EditText box.我有一段代码,我想在用户选择 EditText 框时更改 textView 中显示的文本。

The problem I am having is that the textView only changes when I double click the EditText box, one click and there is no change to the textView.我遇到的问题是 textView 仅在我双击 EditText 框时才会更改,单击一次并且 textView 没有更改。

Is there another click listener that I should be using?我应该使用另一个点击侦听器吗?

final EditText box0105 = (EditText)findViewById(R.id.box0105);
final TextView txtHint = (TextView)findViewById(R.id.txtHint);
        box0105.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtHint.setText(onOneClick);                
        }
    });

在 EditText xml 中添加android:focusableInTouchMode="false"然后 EditText 将接受单击

尝试设置OnTouchListener而不是OnClickListener

use this, it will fire very first.使用它,它会首先触发。

editText.setOnTouchListener(new OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) {
            if(MotionEvent.ACTION_UP == event.getAction())
                // Do whatever you want to do man, but don't trouble your mother
            return false; 
        } 

});

set your edittext in xmlxml设置您的edittext

<EditText
  android:focusable="false"
  ...
/>

I set up 2 listeners one is OnClick and another one is OnFocusChange.我设置了 2 个监听器,一个是 OnClick,另一个是 OnFocusChange。 Like this:像这样:

editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.println("Edittext has been clicked");
    }
});

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        System.out.println("Edittext has been clicked");
    }
});

OnFocus is called when the Edittext is clicked first time and then future times OnClick is called. OnFocus 在第一次单击 Edittext 时被调用,然后在以后的时间 OnClick 被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM