Android UI component-textview and its subclass (3) editview

Source: Internet
Author: User

Editview and textview are similar in usage, but the text can be edited.

TIPS:

Set edittext to hide the keyboard
(Edittext) mMarket. setinputtype (0 );


Setting edittext is not covered by the Input Method
Getwindow (). setsoftinputmode (windowmanager. layoutparams. soft_input_adjust_resize );


Error prompt box (required for verification ):

Seterror ();


If you want to add various effects to the text content in the code, use spannablestring

The Android system uses the spannablestring class to process specified text. Specific functions include:

1. backgroundcolorspan background color

Spannablestring spantext = new spannablestring ("background text"); spantext. setspan (New backgroundcolorspan (color. green), 0, spantext. length (), spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

2. clickablespan text clickable, with click events

Textview TV = findviewbyid (R. id. TV _click); spannablestring spstr = new spannablestring ("clicked text"); clickspan = new nolineclickspan (VO); // set the spstr hyperlink. setspan (clickspan, 0, str. length (), spanned. span_inclusive_exclusive); TV. append (spstr); TV. setmovementmethod (linkmovementmethod. getinstance (); // set the hyperlink to clickable TV. setmovementmethod (linkmovementmethod. getinstance ());

3. foregroundcolorspan text color (foreground color)

Spantext = new spannablestring ("color text"); spantext. setspan (New foregroundcolorspan (color. blue), 6, spantext. length (), spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

4. maskfilterspan modifier effects, such as blurmaskfilter and embossmaskfilter)

Spantext = new spannablestring ("maskfilterspan"); int length = spantext. length (); // blur (blurmaskfilter) maskfilterspan = new maskfilterspan (New blurmaskfilter (3, blur. outer); spantext. setspan (maskfilterspan, 0, length-10, spannable. span_inclusive_exclusive); // embossmaskfilter maskfilterspan = new maskfilterspan (New embossmaskfilter (new float [] {, 3}, 1.5f, 8, 3); spantext. setspan (maskfilterspan, length-10, length, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

5. metricaffectingspan parent class, generally not used


6. rasterizerspan grating effect

spanText = new SpannableString("StrikethroughSpan");spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

7. strikethroughspan strikethrough)

spanText = new SpannableString("StrikethroughSpan");spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);


8. suggestionspan is equivalent to a placeholder which is generally used in the edittext input box. When you double-click the text, a prompt box will pop up to select some recommended text. The selected text will replace the placeholder. Most of the input methods are used.


9. underlinespan underline

spanText = new SpannableString("UnderlineSpan");spanText.setSpan(new UnderlineSpan(), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

10. absolutesizespan absolute size (text font)

spanText = new SpannableString("AbsoluteSizeSpan");spanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

11. Set the Image Based on the text baseline or bottom alignment in dynamicdrawablespan.

DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {    @Override    public Drawable getDrawable() {        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);        d.setBounds(0, 0, 50, 50);        return d;    }};DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {    @Override    public Drawable getDrawable() {          Drawable d = getResources().getDrawable(R.drawable.ic_launcher);          d.setBounds(0, 0, 50, 50);                return d;            }        };spanText.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);spanText.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

12. imagespan Image

spanText = new SpannableString("ImageSpan");Drawable d = getResources().getDrawable(R.drawable.ic_launcher);</strong>d.setBounds(0, 0, 50, 50);spanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);


13. Relative relativesizespan size (text font)

Spantext = new spannablestring ("relativesizespan"); // parameter proportion: proportional size spantext. setspan (New relativesizespan (2.5f), 3, 4, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

14. replacementspan parent class, generally not used


15. scalexspan scales based on the X axis

Spantext = new spannablestring ("hello"); // parameter proportion: proportional size spantext. setspan (New scalexspan (3.8f), 3, 7, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

16. stylespan Font Style: bold and italic

Spantext = new spannablestring ("stylespan"); // typeface. bold_italic: bold + italic spantext. setspan (New stylespan (typeface. bold_italic), 3, 7, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );


17. subscriptspan subscript (used in mathematical formulas)

spanText = new SpannableString("SubscriptSpan ");spanText.setSpan(new SubscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

18. superscriptspan tagging (used in mathematical formulas)

spanText = new SpannableString("SuperscriptSpan ");spanText.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);mTVText.append("\n");mTVText.append(spanText);

19. textappearancespan text appearance (including font, size, style, and color)

Spantext = new spannablestring ("textappearancespan"); // to customize textappearance, you can modify the spantext in the system style. setspan (New textappearancespan (this, android. r. style. textappearance_medium), 6, 7, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );


20. typefacespan text font

Spantext = new spannablestring ("typefacespan"); // to use a Custom font, you may need to override the typefacespanspantext class. setspan (New typefacespan ("monospace"), 3, 10, spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext );

21. urlspan text hyperlink

Spantext = new spannablestring ("urlspan"); spantext. setspan (New urlspan ("http://orgcent.com"), 10, spantext. length (), spannable. span_inclusive_exclusive); mtvtext. append ("\ n"); mtvtext. append (spantext); // enables urlspan to click mtvtext. setmovementmethod (New linkmovementmethod ());


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.