EditText onClickListener in Android

后端 未结 14 2061
时光取名叫无心
时光取名叫无心 2020-12-02 12:03

I want an EditText which creates a DatePicker when is pressed. So I write the following code:

    mEditInit = (EditText) findViewById(R.id.date_init);
    mE         


        
相关标签:
14条回答
  • 2020-12-02 12:12

    I had this same problem. The code is fine but make sure you change the focusable value of the EditText to false.

    <EditText
    android:id="@+id/date"
    android:focusable="false"/>
    

    I hope this helps anyone who has had a similar problem!

    0 讨论(0)
  • 2020-12-02 12:15

    Nice topic. Well, I have done so. In XML file:

    <EditText
        ...
        android:editable="false"
        android:inputType="none" />
    

    In Java-code:

    txtDay.setOnClickListener(onOnClickEvent);
    txtDay.setOnFocusChangeListener(onFocusChangeEvent);
    
    private View.OnClickListener onOnClickEvent = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dpDialog.show();
        }
    };
    private View.OnFocusChangeListener onFocusChangeEvent = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                dpDialog.show();
        }
    };
    
    0 讨论(0)
  • 2020-12-02 12:17

    The keyboard seems to pop up when the EditText gains focus. To prevent this, set focusable to false:

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

    This behavior can vary on different manufacturers' Android OS flavors, but on the devices I've tested I have found this to to be sufficient. If the keyboard still pops up, using hints instead of text seems to help as well:

    myEditText.setText("My text");    // instead of this...
    myEditText.setHint("My text");    // try this
    

    Once you've done this, your on click listener should work as desired:

    myEditText.setOnClickListener(new OnClickListener() {...});
    
    0 讨论(0)
  • 2020-12-02 12:18

    If you use OnClick action on EditText like:

    java:

    mEditInit = (EditText) findViewById(R.id.date_init);
    mEditInit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DATEINIT_DIALOG);
        }
    
    });
    

    or kotlin:

    editTextChooseDate.setOnClickListener {
            showDialog(DATEINIT_DIALOG)
        }
    

    So, it will work perfectly if you put into xml of your EditText the following lines:

    android:inputType="none"
    android:focusable="false"
    android:cursorVisible="false"

    For example:

    <EditText
                android:id="@+id/date_init" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="Select Date"
                android:inputType="none"
                android:focusable="false"
                android:cursorVisible="false"/>
    

    or for MaterialDesign

    <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layoutEditTextChooseDate"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent">
    
        <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/date_init" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="Select Date"
                android:inputType="none"
                android:focusable="false"                      
                android:cursorVisible="false"/>
    
    </com.google.android.material.textfield.TextInputLayout>
    
    0 讨论(0)
  • 2020-12-02 12:18

    The problem with solutions using OnFocusChangeListener is that they interpret any focus gain as a click. This is not 100% correct: your EditText might gain focus from something else than a click.

    If you strictly care about click and want to detect click consistently (regardless of focus), you can use a GestureDetector:

    editText.setOnConsistentClickListener { /* do something */ }
    
    fun EditText.setOnConsistentClickListener(doOnClick: (View) -> Unit) {
        val gestureDetector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
            override fun onSingleTapUp(event: MotionEvent?): Boolean {
                doOnClick(this@setOnConsistentClickListener)
                return false
            }
        })
    
        this.setOnTouchListener { _, motionEvent -> gestureDetector.onTouchEvent(motionEvent) }
    }
    
    0 讨论(0)
  • 2020-12-02 12:23

    IMHO I disagree with RickNotFred's statement:

    Popping a dialog when an EditText gets focus seems like a non-standard interface.

    Displaying a dialog to edit the date when the use presses the an EditText is very similar to the default, which is to display a keyboard or a numeric key pad. The fact that the date is displayed with the EditText signals to the user that the date may be changed. Displaying the date as a non-editable TextView signals to the user that the date may not be changed.

    0 讨论(0)
提交回复
热议问题