指定文本颜色以文字的EditText(Assigning text color to text in

2019-06-23 19:42发布

如何分配不同的色彩通过扩展它EDITTEXT为文本。

Answer 1:

使用跨度:

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


Answer 2:

您可以通过添加修改文字颜色, android:textColor是这样的:

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#f00" />


Answer 3:

我知道这是一个非常古老的问题,但因为这是我第一次搜索时得到的,我想补充一个答案,使未来的读者得到他们。

我们可以创建在styles.xml文件我们自己的自定义风格,除了文字颜色许多其他属性和适用于我们的EditText。

在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>

然后简单地应用这种风格到的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>

简单吧。 :)重要的部分是如果需要,我们可以使用同样的东西更多的EditText的。 :)



Answer 4:

我有问题,在style.xml文件中使用文字颜色属性:

        <item name="android:textColor">@color/white</item>

对我来说editTextColor工作:

        <item name="android:editTextColor">@color/white</item>


Answer 5:

从XML布局:

我们可以改变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>

从Java类:

我们可以改变EditText加入法务实文本颜色EditText.setTextColor()如下:

EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setTextColor(Color.RED);


Answer 6:

以编程方式改变它,在你colors.xml文件中添加颜色

colors.xml:

<resources>
    <color name="colorText">#424242</color>

而像这样引用它:

myEditText.setTextColor(ContextCompat.getColor(this, R.color.colorText));


Answer 7:

机器人:文字颜色,设置XML的EditText此属性。



文章来源: Assigning text color to text in edittext