Android:设置EditText的默认提示颜色来自.java

问题描述:

所以我有一个LinearLayout和4个EditText-s,它带有灰色的XML提示颜色。我有一个按钮,可以动态地将新的EditText-s添加到LinearLayout。问题是当我使用setHint(“文本”)时,它使新创建的视图的提示颜色变黑。Android:设置EditText的默认提示颜色来自.java

也试过setHintTextColor(),但它通过设置自定义颜色为我工作的唯一方法。是否有一个默认的提示颜色,我可以通过setHintTextColor()设置?或者可能是某种方法在被调用时执行它?

代码如下所示:

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; 
} 

附:我制作了一种叫做暗色的新颜色

我一直在寻找解决方案,但没有任何东西能够帮助我,或者我只是不理解它。我在android和编程方面很新,所以请不要评判,只要解释一下。非常感谢

+0

https://*.com/questions/6438478/sethinttextcolor-in-edittext你看过这篇文章吗? – Merijn

+0

尝试过,但它说不能解决“白”,也许将其添加到颜色会有所帮助,但我仍然需要正确的颜色代码 – DaggerTok

它可能为时已晚,但对于其他人谁也有同样问题的缘故,我通过提出一个方法来获得默认textColorHint解决它。
它返回当前主题中指定的所有状态(禁用,聚焦,选定...)的提示文本的颜色。

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; 
} 

我希望这有助于。