以编程方式更改 TextInputEditText 的 HintTextColor 和光标颜色
Change HintTextColor and Cursor Color of TextInputEditText Programmatically
我在我的应用程序中使用 Spinner 和 TextInputEditText,我想在用户选择微调项时更改一些 EditText 功能(光标和提示文本颜色)。这是我的代码:
private void setSpinner(){
String [] types = {"q_a", "attention", "reminder", "explain", "important", "image", "voice"};
TypeAdapter adapter = new TypeAdapter(getActivity(), types);
spinner.setAdapter(adapter);
int [] colors = {
R.color.actual_purple, R.color.actual_pink, R.color.actual_orange,
R.color.actual_carrot, R.color.actual_green,
R.color.deep_blue_grey_flat_icon, R.color.deep_blue_grey_flat_icon
};
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextEditor(colors[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@SuppressLint("NewApi")
private void setTextEditor(int color){
Drawable unwrappedDrawable = AppCompatResources.getDrawable(getActivity(), R.drawable.important_cursor);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(getActivity(), color));
pointEditor.setTextCursorDrawable(unwrappedDrawable);
pointEditorLayout.setBoxBackgroundColor(ContextCompat.getColor(getActivity(), color));
pointEditor.setHintTextColor(ContextCompat.getColor(getActivity(), color));
}
我不明白为什么这不起作用。有人可以帮我吗?
对于 editText 尝试
pointEditor.setHintTextColor(getResources().getColor(color));
对于光标颜色,请查看此 answer
对于那些使用 TextInputLayout 的人,我找到了答案。只需简单地使用:
textInputLayout.setHintTextColor(ColorStateList.valueOf(your_color));
textInputLayout.setBoxStrokeColor(your_color);
对于光标:
editText.setTextCursorDrawable();
我在我的应用程序中使用 Spinner 和 TextInputEditText,我想在用户选择微调项时更改一些 EditText 功能(光标和提示文本颜色)。这是我的代码:
private void setSpinner(){
String [] types = {"q_a", "attention", "reminder", "explain", "important", "image", "voice"};
TypeAdapter adapter = new TypeAdapter(getActivity(), types);
spinner.setAdapter(adapter);
int [] colors = {
R.color.actual_purple, R.color.actual_pink, R.color.actual_orange,
R.color.actual_carrot, R.color.actual_green,
R.color.deep_blue_grey_flat_icon, R.color.deep_blue_grey_flat_icon
};
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextEditor(colors[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@SuppressLint("NewApi")
private void setTextEditor(int color){
Drawable unwrappedDrawable = AppCompatResources.getDrawable(getActivity(), R.drawable.important_cursor);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, ContextCompat.getColor(getActivity(), color));
pointEditor.setTextCursorDrawable(unwrappedDrawable);
pointEditorLayout.setBoxBackgroundColor(ContextCompat.getColor(getActivity(), color));
pointEditor.setHintTextColor(ContextCompat.getColor(getActivity(), color));
}
我不明白为什么这不起作用。有人可以帮我吗?
对于 editText 尝试
pointEditor.setHintTextColor(getResources().getColor(color));
对于光标颜色,请查看此 answer
对于那些使用 TextInputLayout 的人,我找到了答案。只需简单地使用:
textInputLayout.setHintTextColor(ColorStateList.valueOf(your_color));
textInputLayout.setBoxStrokeColor(your_color);
对于光标:
editText.setTextCursorDrawable();