how to make hint disappear when edittext is touche

2019-01-22 06:07发布

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 write something. How can this problem be fixed? Because I need the hint to disappear when the EditText field is touched.

It is for example:

<EditText
    android:layout_width="260dp"
    android:layout_height="30dp"
    android:ems="10"
    android:inputType="text"
    android:id="@+id/driv_lic_num_reg"
    android:hint="@string/driver_license_numb"
    android:textColor="@color/black"
    android:textColorHint="@color/grey_hint"
    android:background="@drawable/input_field_background"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="2dp"
    android:textCursorDrawable="@color/black"
    android:textSize="15dp"
    android:paddingLeft="10dp"
    android:nextFocusDown="@+id/pass_reg"
    android:imeOptions="actionNext"
    android:maxLines="1"
    android:maxLength="50"
    android:focusableInTouchMode="true"/>

11条回答
The star\"
2楼-- · 2019-01-22 06:48

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"
查看更多
Juvenile、少年°
3楼-- · 2019-01-22 06:51

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);
                    }
                }
            }
        });
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-22 06:54
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");
            }
        });
查看更多
Viruses.
5楼-- · 2019-01-22 06:54

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
        }
    }
查看更多
Anthone
6楼-- · 2019-01-22 06:55

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

查看更多
登录 后发表回答