Android 为 EditText 中的文本指定文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10242274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning text color to text in EditText
提问by Chaitanya
How do I assign a different color to text in EditText by extending it?
如何通过扩展它为 EditText 中的文本分配不同的颜色?
采纳答案by Buda Gavril
use spans:
使用跨度:
TextView textView = (TextView)findViewById(R.id.mytextview01);
Spannable WordtoSpan = new SpannableString("partial colored text");
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);
回答by C. Leung
you can change the text color by adding android:textColor
like this:
您可以通过添加android:textColor
这样的方式更改文本颜色:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00" />
回答by Atul O Holic
I know this is a very old question but since this was the first I got while searching, I would like to add an answer so that future readers get them.
我知道这是一个非常古老的问题,但由于这是我在搜索时遇到的第一个问题,我想添加一个答案,以便未来的读者能够得到它们。
We can create our own custom style in styles.xml file with many other attributes in addition to textColor and apply to our EditText.
我们可以在styles.xml 文件中创建我们自己的自定义样式,除了textColor 之外还有许多其他属性,并应用于我们的EditText。
Add this in styles.xml,
在styles.xml中添加这个,
<style name="MyEditTextstyle">
<item name="android:textColor">@color/dark_blue</item>
<item name="android:background">@drawable/my_custom_edittext_bg</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
//many more per requirement
</style>
And then simply apply this style to the EditText as,
然后简单地将此样式应用到 EditText 中,
<EditText
style="@style/MyEditTextstyle" //here
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Item Name"
android:padding="10dp" >
<requestFocus />
</EditText>
Simple, right. :) Important part is we can apply the same stuff to any more EditText's if needed. :)
简单,对。:) 重要的是,如果需要,我们可以将相同的内容应用于更多 EditText。:)
回答by live-love
To change it programatically, add a color in your colors.xml file
要以编程方式更改它,请在您的 colors.xml 文件中添加颜色
colors.xml:
颜色.xml:
<resources>
<color name="colorText">#424242</color>
and reference it like this:
并像这样引用它:
myEditText.setTextColor(ContextCompat.getColor(this, R.color.colorText));
回答by mr.kostua
I had problems with using textColor attribute in style.xml file:
我在 style.xml 文件中使用 textColor 属性时遇到问题:
<item name="android:textColor">@color/white</item>
For me editTextColor works :
对我来说,editTextColor 有效:
<item name="android:editTextColor">@color/white</item>
回答by Rohit Suthar
From XML Layout :
从 XML 布局:
We can change EditText
text color by adding android:textColor
as below :
我们可以EditText
通过添加android:textColor
以下内容来更改文本颜色:
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your Name"
android:textColor="#FF0000"
</EditText>
From Java Class :
从 Java 类:
We can change EditText
text color pragmatically by adding method EditText.setTextColor()
as below :
我们可以EditText
通过添加EditText.setTextColor()
如下方法来实用地更改文本颜色:
EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setTextColor(Color.RED);
回答by MrWaqasAhmed
android:textColor, set this property in EditText xml.
android:textColor,在 EditText xml 中设置此属性。
回答by Anubhav
<EditText
android:layout_width="what_ever_size"
android:layout_height="what_ever_size"
android:textColor="@color/your_choice_of_color" />