Android:从.java设置EditText的默认提示颜色 [英] Android: Setting EditText's default hint color from .java
问题描述
所以我有一个LinearLayout和4个EditText-s,其中XML带有灰色的提示颜色.我有一个按钮,可以将新的EditText-s动态添加到LinearLayout.问题是当我使用setHint("text")时,它将使新创建的视图的提示颜色变为黑色.
So I have a LinearLayout and 4 EditText-s in it with grayish hint colors in XML. And I have button that dynamically adds new EditText-s to LinearLayout. The problem is when I use setHint("text") it makes hint color for new created views black.
也尝试过setHintTextColor(),但是它是通过设置自定义颜色来为我工作的唯一方法.是否可以通过setHintTextColor()设置默认的提示颜色?或某种方法在调用时会执行此操作?
Also tried setHintTextColor() but the only way it worked for me by setting custom color. Is there a default hint color that I can set by setHintTextColor()?? or maybe some method that does it when it's called?
代码如下:
private EditText createNewTextView(String text) {
++x;
final ActionBar.LayoutParams lparams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(this);
editText.setLayoutParams(lparams);
editText.setHint("Name" + x);
editText.setHintTextColor(getResources().getColor(R.color.hintcolor));
return editText;
}
p.s.我用一种新的颜色制成了一种叫做提示色"的颜色
p.s. I made new color in colors which is called hintcolor
我一直在寻找解决方案,但是没有什么对我有帮助,或者我只是不理解.我是android和编程方面的新手,请不要判断,请解释一下.非常感谢
I've been looking for solution, but there was nothing that would help me, or I just didn't understood it. I'm new at android and programming so don't judge please, just explain. Thanks a lot
推荐答案
可能为时已晚,但是为了其他有相同问题的人,我通过制定一种获取默认 textColorHint 的方法来解决了该问题. strong>.
它返回当前主题中指定的所有状态(禁用,聚焦,选择...)的提示文本的颜色.
It may be too late but for the sake of others who have the same problem, I solved it by making a method to get default textColorHint.
It returns the color of the hint text for all the states (disabled, focussed, selected...) that specified in the current theme.
int getHintTextColor(Context context) {
int[] hintTextColor = new int[] { android.R.attr.textColorHint };
int indexOfAttrTextColorHint = 0;
TypedArray ta = context.obtainStyledAttributes(hintTextColor);
int textColor = ta.getColor(indexOfAttrTextColorHint, 0xFF808080);
ta.recycle();
return textColor;
}
我希望这会有所帮助.
这篇关于Android:从.java设置EditText的默认提示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!