How to assign different color to text in Edittext by extending it.
问题:
回答1:
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);
回答2:
you can change the text color by adding android:textColor
like this:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00" />
回答3:
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.
Add this in 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
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. :)
回答4:
From XML Layout :
We can change EditText
text color by adding android:textColor
as below :
<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 :
We can change EditText
text color pragmatically by adding method EditText.setTextColor()
as below :
EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setTextColor(Color.RED);
回答5:
I had problems with using textColor attribute in style.xml file:
<item name="android:textColor">@color/white</item>
For me editTextColor works :
<item name="android:editTextColor">@color/white</item>
回答6:
android:textColor, set this property in EditText xml.
回答7:
To change it programatically, add a color in your colors.xml file
colors.xml:
<resources>
<color name="colorText">#424242</color>
and reference it like this:
myEditText.setTextColor(ContextCompat.getColor(this, R.color.colorText));