Cách gán màu khác nhau cho văn bản trong Edittext bằng cách mở rộng nó.Gán màu văn bản cho văn bản trong edittext
Trả lời
sử dụng kéo dài:
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);
'setText' có thiếu tham số không? Tôi nghĩ rằng nó cũng có một 'BufferType'. –
android: textColor, đặt thuộc tính này trong EditText xml.
bạn có thể thay đổi màu sắc văn bản bằng cách thêm android:textColor
như thế này:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00" />
Tôi biết đây là một câu hỏi rất cũ nhưng vì đây là lần đầu tiên tôi nhận được khi tìm kiếm, tôi muốn thêm một câu trả lời để người đọc trong tương lai có được chúng.
Chúng tôi có thể tạo kiểu tùy chỉnh của riêng mình trong tệp styles.xml với nhiều thuộc tính khác ngoài textColor và áp dụng cho EditText của chúng tôi.
Thêm này trong 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>
Và sau đó chỉ cần áp dụng phong cách này cho EditText như,
<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>
đơn giản, phải không. :) Phần quan trọng là chúng ta có thể áp dụng cùng một thứ cho bất kỳ EditText nào nếu cần. :)
Từ Layout XML:
Chúng tôi có thể thay đổi EditText
màu chữ bằng cách thêm android:textColor
như sau:
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your Name"
android:textColor="#FF0000"
</EditText>
Từ Java Class:
Chúng ta có thể thay đổi màu EditText
văn bản thực tế bằng addi ng phương pháp EditText.setTextColor()
như sau:
EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setTextColor(Color.RED);
Vui lòng đợi một số nghiên cứu trước khi đăng câu hỏi ở đây .. – Ghost