how to make hint disappear when edittext is touched?

后端 未结 12 1143
青春惊慌失措
青春惊慌失措 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:46

    You can also custom your XML code by using Selector. Here is an example code.

    Create a selector. In file selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:color="@android:color/transparent" />
        <item android:color="@color/gray" />
    </selector>
    

    In your view

    android:textColorHint="@drawable/selector"
    
    0 讨论(0)
  • 2020-12-14 00:50

    The way I like to do it (now in Kotlin) is to have the hint reappear if the user clicks elsewhere. Initialize the hint-string outside of your class, also importing your synthetic layout there:

    import kotlinx.android.synthetic.main.activity_layout.*
    private const val MY_HINT= "The Hint Shown"
    

    Then use an OnFocusChange listener:

        myEditText.setOnFocusChangeListener() { v, event ->
            myEditText.hint = if(myEditText.hasFocus()) "" else MY_HINT
            false
        }
    

    That way the hint doesn't get lost as the user clicks around. Or, just set it to an empty string if you don't care about that!

    0 讨论(0)
  • 2020-12-14 00:51

    This is my solution:

    final EditText editText = (EditText) findViewById(R.id.activity_edittext);
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                editText .setHint("my little hint");
            } else {
                editText .setHint("");
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-14 00:55

    My solution is like this. It shows hint when empty and not focused and hides if there is text or if it is touched

    fun hideHint() {
            tilFloating.isHintAnimationEnabled = false
            etFloating.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
                if ((etFloating.text != null && etFloating.text.toString() != "") || hasFocus)
                    tilFloating.hint = ""
                else
                    tilFloating.hint = mHint
            }
        }
    
    0 讨论(0)
  • 2020-12-14 00:56

    Here's my solution, where the hint disappear when user focus editText and appears again when focus changes to other place if the edittext is still empty:

    editText.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            editText.setHint("");
            return false;
        }
    });
    
    editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                editText.setHint("Hint");
            }
        }
    });
    

    hope this helps someone

    0 讨论(0)
  • 2020-12-14 00:58
    findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideKeyboard();
                    editText.clearFocus();
                    if (TextUtils.isEmpty(editText.getText().toString())) {
                        editText.setHint("your hint");
                    }
                }
            });
    
            editText.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    editText.setHint("");
                    return false;
                }
            });
    
    
            editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus)
                        editText.setHint("");
                    else
                        editText.setHint("your hint");
                }
            });
    
    0 讨论(0)
提交回复
热议问题