生活随笔
收集整理的這篇文章主要介紹了
【转】android 中如何限制 EditText 最大输入字符数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文網(wǎng)址:http://blog.csdn.net/fulinwsuafcie/article/details/7437768
方法一:
在 xml 文件中設(shè)置文本編輯框?qū)傩宰髯址麛?shù)限制
如:android:maxLength="10" 即限制最大輸入字符個(gè)數(shù)為10
?
方法二:
在代碼中使用InputFilter 進(jìn)行過濾
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大輸入字符數(shù)為20
?
?
[java]?view plaincopy
public?class?TextEditActivity?extends?Activity?{??????????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);??????????setContentView(R.layout.main);????????????????????EditText?editText?=?(EditText)findViewById(R.id.entry);??????????editText.setFilters(new?InputFilter[]{new?InputFilter.LengthFilter(20)});??????}??}??
方法三:
利用 TextWatcher 進(jìn)行監(jiān)聽
?
[java]?view plaincopy
package?cie.textEdit;????import?android.text.Editable;??import?android.text.Selection;??import?android.text.TextWatcher;??import?android.widget.EditText;????public?class?MaxLengthWatcher?implements?TextWatcher?{????????private?int?maxLen?=?0;??????private?EditText?editText?=?null;??????????????????public?MaxLengthWatcher(int?maxLen,?EditText?editText)?{??????????this.maxLen?=?maxLen;??????????this.editText?=?editText;??????}????????public?void?afterTextChanged(Editable?arg0)?{????????????????????????}????????public?void?beforeTextChanged(CharSequence?arg0,?int?arg1,?int?arg2,??????????????int?arg3)?{????????????????????????}????????public?void?onTextChanged(CharSequence?arg0,?int?arg1,?int?arg2,?int?arg3)?{??????????????????Editable?editable?=?editText.getText();??????????int?len?=?editable.length();????????????????????if(len?>?maxLen)??????????{??????????????int?selEndIndex?=?Selection.getSelectionEnd(editable);??????????????String?str?=?editable.toString();??????????????????????????String?newStr?=?str.substring(0,maxLen);??????????????editText.setText(newStr);??????????????editable?=?editText.getText();????????????????????????????????????????int?newLen?=?editable.length();??????????????????????????if(selEndIndex?>?newLen)??????????????{??????????????????selEndIndex?=?editable.length();??????????????}??????????????????????????Selection.setSelection(editable,?selEndIndex);????????????????????????}??????}????}??
對(duì)應(yīng)的 activity 部分的調(diào)用為:
?
?
[java]?view plaincopy
package?cie.textEdit;????import?android.app.Activity;??import?android.os.Bundle;??import?android.text.InputFilter;??import?android.widget.EditText;????public?class?TextEditActivity?extends?Activity?{??????????@Override??????public?void?onCreate(Bundle?savedInstanceState)?{??????????super.onCreate(savedInstanceState);??????????setContentView(R.layout.main);????????????EditText?editText?=?(EditText)?findViewById(R.id.entry);??????????editText.addTextChangedListener(new?MaxLengthWatcher(10,?editText));????????}??}?? 限制輸入字符數(shù)為10個(gè)
?
main.xml 文件
?
[html]?view plaincopy
<?xml?version="1.0"?encoding="utf-8"?>??<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="fill_parent"??????android:layout_height="fill_parent">??????<TextView??????????android:id="@+id/label"??????????android:layout_width="fill_parent"??????????android:layout_height="wrap_content"??????????android:text="Type?here:"/>??????<EditText??????????android:id="@+id/entry"??????????android:singleLine="true"??????????android:layout_width="fill_parent"??????????android:layout_height="wrap_content"??????????android:background="@android:drawable/editbox_background"??????????android:layout_below="@id/label"/>??????<Button??????????android:id="@+id/ok"??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:layout_below="@id/entry"??????????android:layout_alignParentRight="true"??????????android:layout_marginLeft="10dip"??????????android:text="OK"?/>??????<Button??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:layout_toLeftOf="@id/ok"??????????android:layout_alignTop="@id/ok"??????????android:text="Cancel"?/>??</RelativeLayout>??
效果為輸入了10個(gè)字符后,光標(biāo)停在末尾
?
總結(jié)
以上是生活随笔為你收集整理的【转】android 中如何限制 EditText 最大输入字符数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。