EditText onClickListener in Android

后端 未结 14 2063
时光取名叫无心
时光取名叫无心 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:38

    Default working of EditText: On first click it focuses and on second click it handles onClickListener so you need to disable focus. Then on first click the onClickListener will handle.

    To do that you need to add this android:focusableInTouchMode="false" attribute to your EditText. That's it!

    Something like this:

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="false"
            android:inputType="text" />
    
    0 讨论(0)
  • 2020-12-02 12:39

    Here is what worked for me

    Set editable to false

    <EditText android:id="@+id/dob"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Date of Birth"
    android:inputType="none"
    android:editable="false"
    

    />

    Then add an event listener for OnFocusChange

    private  View.OnFocusChangeListener onFocusChangeDOB= new View.OnFocusChangeListener() {
    
    @Override
     public void onFocusChange(View v, boolean hasFocus) {
       if (hasFocus){
         showDialog(DATE_DIALOG_ID);
       }
     }
    };
    
    0 讨论(0)
提交回复
热议问题