Android font color and EditText automatic input can be converted to uppercase/lowercase letters.

Source: Internet
Author: User

You can use the java class SpannableString and Html languages to set the font color of a word in TextView to the specified color.

(1) SpannableString class

private void setText(TextView t){String text = t.getText().toString().trim();SpannableString span = new SpannableString(text);span.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);t.setText(span);}

(2) html Markup Language

String html1 ="* Name:"; TextView. setText (Html. fromHtml (html1 ));

When lowercase letters are entered, the EditText edit box is automatically converted to uppercase letters. There are also multiple ways to set listening for an EditText object and call the setTransformationMethod () method of the EditText object.

(1) set listening for EditText

MEdtLicensePlateNumber. addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {// TODO Auto-generated method stubmEdtLicensePlateNumber. removeTextChangedListener (this); // cancel the mEdtLicensePlateNumber event. setText (s. toString (). toUpperCase (); // convert mEdtLicensePlateNumber. setSelection (s. toString (). length (); // reset the cursor position mEdtLicensePlateNumber. addTextChangedListener (this); // rebind licensePlateNumber = mEdtLicensePlateNumber. getText (). toString (). trim () ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {// TODO Auto-generated method stub} @ Overridepublic void afterTextChanged (Editable s) {// TODO Auto-generated method stub }});

(2) Call the setTransformationMethod () method of the EditText object
public class InputLowerToUpper extends ReplacementTransformationMethod{@Overrideprotected char[] getOriginal() {char[] lower = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };return lower;}@Overrideprotected char[] getReplacement() {char[] upper = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };return upper;}}editext.setTransformationMethod(new InputLowerToUpper());


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.