日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【转】android 中如何限制 EditText 最大输入字符数

發布時間:2023/11/29 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】android 中如何限制 EditText 最大输入字符数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文網址:http://blog.csdn.net/fulinwsuafcie/article/details/7437768

方法一:

在 xml 文件中設置文本編輯框屬性作字符數限制

如:android:maxLength="10" 即限制最大輸入字符個數為10

?

方法二:

在代碼中使用InputFilter 進行過濾

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大輸入字符數為20

?

?

[java]?view plaincopy
  • public?class?TextEditActivity?extends?Activity?{??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@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 進行監聽

    ?

    [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)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ??????????
  • ????}??
  • ??
  • ????public?void?beforeTextChanged(CharSequence?arg0,?int?arg1,?int?arg2,??
  • ????????????int?arg3)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ??????????
  • ????}??
  • ??
  • ????public?void?onTextChanged(CharSequence?arg0,?int?arg1,?int?arg2,?int?arg3)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????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);??
  • ??????????????
  • ????????}??
  • ????}??
  • ??
  • }??

  • 對應的 activity 部分的調用為:

    ?

    ?

    [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?{??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@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));??
  • ??
  • ????}??
  • }??
  • 限制輸入字符數為10個

    ?

    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個字符后,光標停在末尾

    ?



    總結

    以上是生活随笔為你收集整理的【转】android 中如何限制 EditText 最大输入字符数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。