TextInputLayout and EditText double hint issue

后端 未结 7 2281
生来不讨喜
生来不讨喜 2021-02-11 19:34

I want to set the hint with java in EditText(which is in TextInputLayout).

Code used for setting hint:

aET = (EditText) findViewById(R.id.

相关标签:
7条回答
  • 2021-02-11 20:13

    You can go by using two hint texts. One for the TextInputLayout and other for the edit text inside it.Below is the reference:

    <android.support.design.widget.TextInputLayout
                                        style="@style/editText_layout"
                                        android:id="@+id/entryScreenProduction_editText_percentCompleted_layout"
                                        android:hint="%Completed">
    
                                        <EditText
                                            android:id="@+id/entryScreenProduction_editText_percentCompleted"
                                            style="@style/editTextNotes"
                                            android:gravity="center_vertical|top"
                                            android:inputType="numberDecimal"
                                            android:textAlignment="viewStart"
                                            android:hint="0.0"/>
    </android.support.design.widget.TextInputLayout>
    

    But this has a problem. The UI will look like below. The hints will overlap.

    To avoid the above ugly UI, you have to control this from program.Set the hint text of the EditText only when there is focus on the field, else remove the hint text.

    android:hint="0.0"
    

    Remove the above line from the layout xml file and do as below:

    m_editText_completed = (EditText) findViewById(R.id.entryScreenProduction_editText_percentCompleted);
    m_editText_completed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(b){
                        m_editText_completed.setHint("0.0");
                    }
                    else {
                        m_editText_completed.setHint("");
                    }
                }
            });
    

    I hope this solves you issue. God Speed !!!

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