【问题标题】:have to click two times to call an onclick method of edittext android必须单击两次才能调用edittext android的onclick方法
【发布时间】:2014-01-16 15:49:25
【问题描述】:

我的 android 活动中有这个 editText

         <EditText
            android:id="@+id/payment_expiration"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/payment_expiration_label"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:clickable="true"
            android:onClick="update_expiration_date"
            android:editable="false"
            android:layout_marginTop="-4dp"
            android:cursorVisible="false"
            android:maxLength="7"
            android:padding="10dp"
            android:textSize="13dp" />

正如您在用户点击时看到的那样

我称之为启动 datePickerDialog 的方法:

    public void update_expiration_date(View v){
    Log.i("","cliqué");
    picker.show();
    can_update_expiration_date = true;

}

我遇到的问题是:在我第一次打开这个活动时,用户必须点击两次才能启动对话框

不过之后,一键就够了

我该如何解决这个问题

【问题讨论】:

  • 您的 onClick 监听器在哪里?你有什么?您在哪里以及如何定义选择器?请分享您的代码。
  • 也许我对这个类似问题的回答可能会有所帮助:stackoverflow.com/a/42092582/1617737

标签: android events click android-edittext launch


【解决方案1】:

查看Similar Question

“第一次单击只是将焦点设置到 TextBox,然后第二次单击实际上被视为单击。”

尝试设置android:focusable="false"

【讨论】:

    【解决方案2】:

    您可以尝试可聚焦的解决方案,或者只是添加一个状态跟踪变量...

    public class YourActivity extends Activity {
    
    private boolean clicked;
    
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        clicked = false;
    }
    
    public void update_expiration_date(View v){
        if(clicked == false){ 
           clicked = true;
           return;
        } else {
    
        Log.i("","cliqué");
        picker.show();
        can_update_expiration_date = true;
        }
    
    }
    
    }
    

    【讨论】:

      【解决方案3】:

      begiPass 解决方案有效,但还有更好的解决方案。

      使用 focusable false 用户永远看不到选择了哪个编辑文本。

      更好的解决方案是使用 onTouchListener。我的示例代码:

       edit.setOnTouchListener(new View.OnTouchListener() {
                  @Override
                  public boolean onTouch(View v, MotionEvent event) {
                      edit.setText("");
                      v.setOnTouchListener(null);
                      return false;
                  }
        });
      

      不要忘记将 OnTouchListener 设置为 null。我们只想清除提示(仅一次)。

      问候!

      【讨论】:

        【解决方案4】:

        使用 OnTouchListener 处理它:

                when (view) {
                    edtName -> {
                        if (motionEvent.action == MotionEvent.ACTION_UP) {
                            edtName.requestFocus()
                            //TODO
                        }
                    }
                    edtPassword -> {
                        if (motionEvent.action == MotionEvent.ACTION_UP) {
                            edtPassword.requestFocus()
                           //TODO
                        }
                    }
                }
                return false
            }```
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-29
          • 2012-03-06
          • 1970-01-01
          • 2017-03-07
          • 2012-10-01
          • 1970-01-01
          相关资源
          最近更新 更多