how to make hint disappear when edittext is touched?

后端 未结 12 1144
青春惊慌失措
青春惊慌失措 2020-12-14 00:39

In my application in android are many EditText fields. And I ran into a problem with hint. It is not disappearing when EditText is focused, but it disappears when I start to

相关标签:
12条回答
  • 2020-12-14 00:58
    android:hint="User Name"
    

    This code line can be done that easily way!

    <EditText
      android:id="@+id/edtUserName"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_weight="1"
      android:ems="10"
      android:inputType="textPersonName"
      android:hint="User Name" />
    
    0 讨论(0)
  • 2020-12-14 00:59

    Every Given Solution may work or not I don't know but the Following steps would definitely help.

    First Create a dummy focus to restrain the EditText from gaining focus by

        <LinearLayout
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>
    

    Then Normally set hint in your EditText as example:

        <EditText
    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="text"
        android:ems="10"
        android:id="@+id/editText"
        android:hint="Your Hint"
        android:focusableInTouchMode="true"
       />
    

    Finally add the following code in your activity or fragment where you intend

        editText.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                editText.setHint("");
                return false;
            }
    
        });
    
    0 讨论(0)
  • 2020-12-14 01:03

    Try

    EditText editText = (EditText) findViewById(...);
    editText.setHint("");
    

    in your onTouch logic.
    However your hint must be dissapearing when you clicked on edit text. So re-check your app logic.

    0 讨论(0)
  • 2020-12-14 01:09

    Here i have used the little bit of code to hide and show hint may be it'll be helpful.

    txt_username.setOnFocusChangeListener(new OnFocusChangeListener() {
    
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(txt_username.getText().toString().isEmpty()){
                        if(hasFocus){
                            txt_username.setHint("");
                        }else{
                            txt_username.setHint(R.string.username);
                        }
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-14 01:11

    Hint only disappears when you type in any text, not on focus. I don't think that there is any automatic way to do it, may be I am wrong. However, as a workaround I use the following code to remove hint on focus in EditText

    myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                myEditText.setHint("");
            else
                myEditText.setHint("Your hint");
        }
    });
    
    0 讨论(0)
  • 2020-12-14 01:13

    The proposed selector solution by windyzboy doesn't work for EditTexts with centered text, because the cursor stays behind the transparent hint text instead of moving to the center.

    A good way without polluting your code is to make a custom EditText, since you probably need the same style in the whole application. That way you can avoid adding checks for each EditText separately.

    public class HideHintEditText extends EditText {
    
        CharSequence hint;
    
        public HideHintEditText(Context context) {
            super(context);
            hint = this.getHint();
        }
    
        public HideHintEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            hint = this.getHint();
        }
    
        public HideHintEditText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            hint = this.getHint();
        }
    
        @Override
        protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    
            this.setHint(focused ? "" : hint);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题