Android EditText Hint

后端 未结 4 1909
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 06:40

I have set a hint for an EditText, currently the hint visibility is gone. When a user starts typing, I want to remove the hint text, when the cursor is visible

相关标签:
4条回答
  • 2020-11-30 06:47

    I don't know whether a direct way of doing this is available or not, but you surely there is a workaround via code: listen for onFocus event of EditText, and as soon it gains focus, set the hint to be nothing with something like editText.setHint(""):

    This may not be exactly what you have to do, but it may be something like this-

    myEditText.setOnFocusListener(new OnFocusListener(){
      public void onFocus(){
        myEditText.setHint("");
      }
    });
    
    0 讨论(0)
  • 2020-11-30 07:00

    To complete Sunit's answer, you can use a selector, not to the text string but to the textColorHint. You must add this attribute on your editText:

    android:textColorHint="@color/text_hint_selector"
    

    And your text_hint_selector should be:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:color="@android:color/transparent" />
        <item android:color="@color/hint_color" />
    </selector>
    
    0 讨论(0)
  • 2020-11-30 07:01

    You can use the concept of selector. onFocus removes the hint.

    android:hint="Email"
    

    So when TextView has focus, or has user input (i.e. not empty) the hint will not display.

    0 讨论(0)
  • 2020-11-30 07:05
    et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
    
                et.setHint(temp +" Characters");
            }
        });
    
    0 讨论(0)
提交回复
热议问题