EditText 中的 setHintTextColor()

我有一个视图,其中有两个文本框,用户可以从同一屏幕上的另一个视图中选择文本颜色(通过对话框)。

因此,当用户改变颜色通过对话框,我改变颜色的 EditText文本及其提示。但是,当有一些文本是在 EditText后,该用户选择其他颜色,那么该文本是在该颜色。但是如果我删除所有的文本,那么 提示文本的颜色是以前的颜色。

例如,当前如果我在文本框中有红色,并且用户选择绿色,那么文本就是绿色的。但是如果我删除这个文本,那么提示文本将变成红色,即使我使用代码 改变提示颜色。这个问题只有在那里有一些文本时才会出现。如果是空白的,并且有提示文本,那么问题就不会出现。

108941 次浏览

Use this to change the hint color. -

editText.setHintTextColor(getResources().getColor(R.color.white));

Solution for your problem -

editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){
//do something
}


@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//do something
}


@Override
public void afterTextChanged(Editable arg0) {
if(arg0.toString().length() <= 0) //check if length is equal to zero
tv.setHintTextColor(getResources().getColor(R.color.white));
}
});

Simply add this in your layout for the EditText :

android:textColorHint="#FFFFFF"

Inside Layout Xml File We can Change Color of Hint.....

android:textColorHint="@android:color/*****"

you can replace * with color or color code.

Programmatically in Java - At least API v14+

exampleEditText.setHintTextColor(getResources().getColor(R.color.your_color));

Seems that EditText apply the hintTextColor only if the text is empty. So simple solution will be like this

Editable text = mEditText.getText();
mEditText.setText(null);
mEditText.setHintTextColor(color);
mEditText.setText(text);

If you have multiple fields, you can extend the EditText and write a method which executes this logic and use that method instead.

This is like default hint color, worked for me:

editText.setHintTextColor(Color.GRAY);

Default Colors:

android:textColorHint="@android:color/holo_blue_dark"

For Color code:

android:textColorHint="#33b5e5"

You could call editText.invalidate() after you reset the hint color. That could resolve your issue. Actually the SDK update the color in the same way.