How much of a custom control in Android is left with the edittext of the input characters

Source: Internet
Author: User

Recent projects have a need to determine how many characters left to input, that is, the text changes to EditText to do the monitoring, the function is realized, but feel the use of combination, every time to write, rather than write a custom control to spare. When viewing this article, we recommend that you refer to an article I reproduced: http://blog.csdn.net/android_jiangjun/article/details/39580253

The following is the topic of this article:

First of all, let's look at it:

The custom control in this article takes the form of a combined control to customize the control, and the layout of the custom controls is as follows

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" wrap_content "     android:background=" #fff "        >    <edittext     Android:id= "@+id/edit"    android:layout_width= "match_parent"    android:layout_height= "Wrap_content    " Android:gravity= "Top|left"    android:minlines= "5"       /><textview     android:id= "@+id/text    " Android:layout_width= "Wrap_content"    android:layout_height= "wrap_content"    android:hint= "can also input 25 words"    android:textsize= "10dip"    android:layout_alignbottom= "@id/edit"    android:layout_alignright= "@id/ Edit "    android:layout_marginright=" 3dip "    android:layout_marginbottom=" 3dip "    /></ Relativelayout>

Then the corresponding custom Hintedit class code is as follows:

Package Com.gc.testcustomedittext;import Android.annotation.suppresslint;import Android.content.context;import Android.content.res.typedarray;import Android.text.editable;import Android.text.inputfilter;import Android.text.textwatcher;import Android.util.attributeset;import Android.view.layoutinflater;import Android.widget.edittext;import Android.widget.linearlayout;import Android.widget.relativelayout;import android.widget.textview;/** * * @author Android General * */public Class Hintedittext extends Relativelayout implements Textwat Cher{private EditText medittext;private TextView mtextview;private int maxlength=0;private relativelayout Mrelativelayout, @SuppressLint ("Newapi") public hintedittext (context context, AttributeSet attrs, int defstyle) {super ( Context, attrs, Defstyle);} Public Hintedittext (Context context) {super (context);} Public Hintedittext (context context, AttributeSet Attrs) {Super (context, attrs); TypedArray mtypedarray=context.obtainstyledattributes (Attrs, R.styleable.hintediTtext); Maxlength=mtypedarray.getint (r.styleable.hintedittext_maxlength, 0); mrelativelayout= (RelativeLayout) Layoutinflater.from (context). Inflate (R.layout.custom_edittext, this,true); medittext= (EditText) Mrelativelayout.findviewbyid (R.id.edit); mtextview= (TextView) Mrelativelayout.findviewbyid (R.id.text); Mtextview.sethint ("You can also enter the" +maxlength+ "word"),//Limit the maximum number of characters to enter medittext.setfilters (new inputfilter[]{new Inputfilter.lengthfilter (MaxLength)}); Medittext.addtextchangedlistener (this);} public void Initui (context context) {Relativelayout mrelativelayout= (relativelayout) layoutinflater.from (context). Inflate (R.layout.custom_edittext, this,true); medittext= (EditText) Mrelativelayout.findviewbyid (R.id.edit); mtextview= (TextView) Mrelativelayout.findviewbyid (r.id.text);} @Overridepublic void Beforetextchanged (charsequence s, int start, int count,int after) {//TODO auto-generated method stub } @Overridepublic void OnTextChanged (charsequence s, int start, int before, int count) {//TODO auto-generated method STUBM TExtview.sethint ("can also enter" + (maxlength-s.tostring (). Length ()) + "word");} @Overridepublic void aftertextchanged (Editable s) {//TODO auto-generated method stub}}

The code for the Attrs.xml file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable    name= "Hintedittext" >        <attr name= "maxLength" format= "integer"/>   <!--maxLength as a property of a custom control so that after customizing the property, you can set the property directly in the XML file-- >
</declare-styleable></resources>
The file code for the main layout is as follows

< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:hintedittext= "http// Schemas.android.com/apk/res/com.gc.testcustomedittext "xmlns:tools=" Http://schemas.android.com/tools "Android: Layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <com.gc.t Estcustomedittext. Hintedittext android:id= "@+id/hint_edt" android:layout_width= "match_parent" android:layout_height= "WR Ap_content "hintedittext:maxlength=" "/> <com.gc.testcustomedittext.hintedittext android:    Layout_width= "Match_parent" android:layout_height= "wrap_content" hintedittext:maxlength= ""/> " <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "s Ahjdsahjd "/></LINEARLAYOUT> 

The code for Mainactivity is as follows:

Package Com.gc.testcustomedittext;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import android.view.menuitem;/** *  * @author Android General * */public Class Mainactivity extends Activity {    @Override    protected void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);            }}




This definition control can be used directly, simply by copying the layout file and the Hintedittext class of the custom control to your project directory, and using it like the main layout and mainactivity.

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/39580715

How much of a custom control in Android is left with the edittext of the input characters

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.