Android UI Programming Custom Controls Preliminary (next)--customedittext

Source: Internet
Author: User

Overview:

We also have a preliminary understanding of custom controls based on the previous blog, "Android UI Programming Custom Controls (top)--imagebutton" . So now we can try to do some custom learning about EditText. The following two ways of customizing UI programming are shared with everyone.


Example: an input box with a Delete buttonShow:



Basic prototype Construction:

You can see two things from above: The EditText on the left and the picture on the right (here is a button). When our input in EditText is empty, the clear button on the right is not displayed. Once the content is entered in the EditText, the Clear button on the right side is displayed.


Design and function additions:1. Exterior design

After we had chosen the skeleton, the rest was to dress. Now let's see how to dress it. The following is the sample code:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent "Android:layout_height=" match_parent "android:orientation=" horizontal "> <edittext android:id=" @+id /input_edittext "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:lay Out_centervertical= "true" android:layout_marginleft= "10DP" android:layout_margintop= "5DP" Android:lay Out_marginbottom= "5DP" android:background= "@null" android:layout_toleftof= "@+id/clear_button" Android         : ems= "Ten" > </EditText> <button android:id= "@+id/clear_button" android:layout_width= "30DP" android:layout_height= "30DP" android:layout_centervertical= "true" android:background= "@drawable/clea R_button "android:layout_marginright=" 10DP "android:layout_alignparentright=" true "android:visibility = "Invisible"/></relativeLayout> 

2. Function Add

The addition of functionality is implemented in Java code, because Java code can dynamically adjust the function, but in the XML code does not write the effect of dynamic adjustment function. The following features are implemented in Java code:

Listen to text box features:

public void Addtextchangedlistener () {        Minput.addtextchangedlistener (new Textwatcher () {                        @Override            public void ontextchanged (charsequence s, int start, int before, int count) {                int len = Minput.gettext (). toString (). Le Ngth ();                if (Len > 0) {                    mclear.setvisibility (view.visible);                } else {                    mclear.setvisibility (view.invisible);                }            }                        @Override public            void beforetextchanged (Charsequence s, int. start, int count,                    int after) {            }                        @ Override public            void aftertextchanged (Editable s) {            }        });    


Clear Text function:

private void Setclearevent () {        Mclear.setonclicklistener (new Onclicklistener () {                        @Override public            Void OnClick (View v) {                minput.settext ("");            }        });    }


Usage Analysis:use in 1.xml code

The code is as follows:

<com.demo.customview.clearedittext.widgets.clearedittext        android:id= "@+id/activity_main_clear_edittext"        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:layout_margintop= "10DP"/>

use in 2.Java code

Clearedittext Clearedittext = (clearedittext) Findviewbyid (R.id.activity_main_clear_edittext); Clearedittext.sethint ("Input text for testing"); Clearedittext.addtextchangedlistener ();


Example: inedittext Insert ExpressionShow:



Custom class implementations:

this way, we're not going to voluminous. Because the content is very single. The Java implementation code is as follows:

public class Smiliesedittext extends EditText {public Smiliesedittext (context context) {super (context);    } public Smiliesedittext (context context, AttributeSet Attrs) {Super (context, attrs);    } @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas    Urespec, Heightmeasurespec); The public void Inserticon (int id) {///spannablestring consecutive string, the length is immutable, and you can attach some object, variable words using Spannablestringbuilder, reference        SDK documentation Spannablestring SS = new spannablestring (GetText (). toString () + "[Smile]");        Get the resource to display the picture drawable D = getresources (). getdrawable (ID);        Set height d.setbounds (0, 0, d.getintrinsicwidth (), D.getintrinsicheight ());        The bottom of the span should be aligned with the baseline of the surrounding text imagespan span = new Imagespan (d, Imagespan.align_baseline);              Attach picture Ss.setspan (span, GetText (). Length (),  GetText (). Length () + "[smile]". Length (), spannable.span_inclusive_exclusive);    SetText (ss); }}

Usage Analysis:use in 1.xml code

<com.demo.customview.clearedittext.widgets.smiliesedittext        android:id= "@+id/activity_main_similies_ EditText "        android:layout_width=" fill_parent "        android:layout_height=" wrap_content "        android:layout_ below= "@+id/activity_main_clear_edittext"        android:singleline= "true"        android:text= "Hello Smile"        Android:hint= "You can input the expression oh"        android:textsize= "14sp"        android:layout_margintop= "20DP"/>

use in 2.Java code

Smiliesedittext et= (Smiliesedittext) Findviewbyid (r.id.activity_main_similies_edittext);        Et.inserticon (r.drawable.smile);        System.out.println (Et.gettext (). toString ());


source code Download:

http://download.csdn.net/detail/u013761665/8410583

Android UI Programming Custom Controls Preliminary (next)--customedittext

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.