The number of characters in the Android input box is limited.

Source: Internet
Author: User

The number of characters in the Android input box is limited.

Sometimes there is a limit on the number of characters entered in the Android input box, and the number of characters entered is displayed. You can achieve this by using the following methods:

1. subdefinition LimitNumEditText inherits EditText
Import android. content. context; import android. content. res. typedArray; import android. telephony. smsMessage; import android. text. editable; import android. text. inputFilter; import android. text. textWatcher; import android. util. attributeSet; import android. widget. editText; import us. pinguo. cc. r;/*** Created by crab on 15-3-18. */public class LimitNumEditText extends EditText {private int mMaxLength; private OnTextCountChangeListener listener; public LimitNumEditText (Context context) {this (context, null);} public LimitNumEditText (Context context, AttributeSet attrs) {super (context, attrs); TypedArray typedArray = context. obtainStyledAttributes (attrs, R. styleable. limitNumEditText); mMaxLength = typedArray. getInt (R. styleable. limitNumEditText_maxLength,-1); typedArray. recycle (); if (m MaxLength> = 0) {setFilters (new InputFilter [] {new InputFilter. lengthFilter (mMaxLength)});} else {setFilters (new InputFilter [0]);} addTextChangedListener (null );} /*** @ return limit the maximum number of characters entered */public int getLimitLength () {return mMaxLength;} @ Override public void addTextChangedListener (final TextWatcher watcher) {TextWatcher inner = new TextWatcher () {@ Override public void beforeTextChanged (C HarSequence s, int start, int count, int after) {if (watcher! = Null) {watcher. beforeTextChanged (s, start, count, after) ;}@ Override public void onTextChanged (CharSequence s, int start, int before, int count) {int [] params = SmsMessage. calculateLength (s, false); int use = params [1]; if (mMaxLength> = 0 & mTextCountChangeListener! = Null) {mTextCountChangeListener. onTextCountChange (use, mMaxLength);} if (watcher! = Null) {watcher. onTextChanged (s, start, before, count) ;}@override public void afterTextChanged (Editable s) {if (watcher! = Null) {watcher. afterTextChanged (s) ;}}; super. addTextChangedListener (inner);} public LimitNumEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public void listener (OnTextCountChangeListener listener) {mTextCountChangeListener = listener ;} /*** listen for character changes in the input box */public interface OnTextCountChangeListener {/***** @ param use the size of the input character likes * @ param total limit the number of input online */public void onTextCountChange (int use, int total );}

2. Modify the res/values/attrs. xml file and add the following line <declare-styleable name = "LimitNumEditText">
<Attr name = "maxLength" format = "integer"/>
</Declare-styleable>
3. Use it in the layout File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:algnText="http://schemas.android.com/apk/res-auto"    android:background="#FFFFFF"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.example.crab.mycameratest.LimitNumEditText        algnText:maxLength="20"        android:id="@+id/id_edit_text_test"        android:layout_width="match_parent"        android:layout_height="50dp"/></LinearLayout>



Related Article

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.