Define your own background for drawing Android EditText, define the display style of edittext text

Source: Internet
Author: User

EditText is able to draw the background through Layer-list:

<?

XML version= "1.0" encoding= "Utf-8"?

><layer-list xmlns:android= "http://schemas.android.com/apk/res/android" ><item> <shape Android:shape= "Rectangle"//box for rectangle > <solid android:color= "#FFFFFF"/> //Fill inside with white < Stroke//Border is 1DP line android:width= "1DP" android:color= "#AEAEAE" /> <corners android:radius= " 10DP "/> //rounded corners of the bezel </shape></item></layer-list>


Write layout:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android: background= "#FFFFFF" > <include layout= "@layout/title_list"/> <view android:l ayout_below= "@id/title" android:id= "@+id/line" android:layout_width= "Fill_parent" Android:layout_heig ht= "2dip" android:background= "@drawable/rc_list_divider"/> <relativelayout android:layout_wid Th= "Fill_parent" android:layout_height= "150DP" android:layout_marginleft= "20DP" Android:layout_margin right= "20DP" android:layout_centerinparent= "true" >        <edittext android:id= "@+id/custom_edittext" android:layout_width= "Fill_parent" android:layout_height= "150DP"//is consistent with parent height so left_word_num can android:background= "@drawable/reply_ in the background frame.            Custom_message_edit_background "android:gravity=" left|top "android:hint=" please inputsomething "            Android:paddingbottom= "24DP" android:paddingleft= "12DP" android:paddingright= "12DP" android:paddingtop= "17DP" android:textcolor= "#000000" android:textcolorhint= "#AEAEAE" a Ndroid:textsize= "20sp"/> <textview android:id= "@+id/other_char_hint" Android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbottom= "@id/custom_editt Ext "android:layout_alignparentbottom=" true "android:paddingbottom=" 10DP "android:padding Left= "12DP" android:text= "0" android:textcolor= "#AEAEAE" android:textsize= "14sp" android:visibility= "G One "></TextView> <textview android:id=" @+id/left_word_num "android:layout_width="            Wrap_content "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "            Android:layout_alignparentright= "true" android:paddingbottom= "10DP" android:paddingright= "10DP"     android:text= "android:textcolor=" #000000 "android:textsize=" 14SP "></TextView> </RelativeLayout> </RelativeLayout>

The code implements some edittext changes when dealing with logic (the logic now is to not let input Chinese characters:

Private EditText mcustom_edittext;private TextView left_word_num;private static final int max_input_num = 200;private Tex TView other_char_hint; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.edittext); mCustom_ EditText = (edittext) Findviewbyid (r.id.custom_edittext); other_char_hint = (TextView) Findviewbyid (r.id.other_char_ hint); Other_char_hint.settext ("Only enlish accepted"); Left_word_num = (TextView) Findviewbyid (r.id.left_word_num); Mcustom_edittext.addtextchangedlistener (New Textwatcher () {private charsequence temp;private int selectionstart; private int selectionend; @Overridepublic void ontextchanged (charsequence s, int start, int before, int count) {} @Overridep ublic void Beforetextchanged (charsequence s, int start, int count, int after) {temp = s;} @Overridepublic void aftertextchanged (Editable s) {SelectionStart = Mcustom_edittext.getselectionstart (); Selectionend = MCuStom_edittext.getselectionend ();  String Lastword = s.tostring (). Trim (); if (Lastword.length () > 0) {lastword = Lastword.substring (Lastword.length ()-1, Lastword.length ()), if (!iscontentvalid (temp.tostring ())) {other_char_hint.setvisibility (view.visible); for (int i=0 ; i < temp.tostring (). Length (); i++) {String Temp1 = temp.tostring (). substring (i, i+1); if (!isinputvalid (TEMP1)) {// Use Setspan to draw a black line under the EditText word s.setspan (new Underlinespan (), I, i+1, spanned.span_exclusive_exclusive);}}} Else{other_char_hint.setvisibility (View.gone);}} Else{other_char_hint.setvisibility (View.gone);} Left_word_num.settext (String.valueof (Max_input_num-temp.length ()));});} Private Boolean Iscontentvalid (string content) {for (int i=0;i < content.length (); i++) {String value = Content.substrin g (i,i+1); if (!isinputvalid (value)) {return false;}} return true;} Private Boolean isinputvalid (String s) {byte[] Ch_array = S.getbytes (); if (ch_array.length = = 1) {return true;} else {Retu rn false;}}

:


Define your own background for drawing Android EditText, define the display style of edittext text

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.