zoukankan      html  css  js  c++  java
  • 【转】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
     
    1. public class TextEditActivity extends Activity {  
    2.     /** Called when the activity is first created. */  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(R.layout.main);  
    7.           
    8.         EditText editText = (EditText)findViewById(R.id.entry);  
    9.         editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});  
    10.     }  
    11. }  


    方法三:

    利用 TextWatcher 进行监听

    [java] view plaincopy
     
    1. package cie.textEdit;  
    2.   
    3. import android.text.Editable;  
    4. import android.text.Selection;  
    5. import android.text.TextWatcher;  
    6. import android.widget.EditText;  
    7.   
    8. /* 
    9.  * 监听输入内容是否超出最大长度,并设置光标位置 
    10.  * */  
    11. public class MaxLengthWatcher implements TextWatcher {  
    12.   
    13.     private int maxLen = 0;  
    14.     private EditText editText = null;  
    15.       
    16.       
    17.     public MaxLengthWatcher(int maxLen, EditText editText) {  
    18.         this.maxLen = maxLen;  
    19.         this.editText = editText;  
    20.     }  
    21.   
    22.     public void afterTextChanged(Editable arg0) {  
    23.         // TODO Auto-generated method stub  
    24.           
    25.     }  
    26.   
    27.     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,  
    28.             int arg3) {  
    29.         // TODO Auto-generated method stub  
    30.           
    31.     }  
    32.   
    33.     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {  
    34.         // TODO Auto-generated method stub  
    35.         Editable editable = editText.getText();  
    36.         int len = editable.length();  
    37.           
    38.         if(len > maxLen)  
    39.         {  
    40.             int selEndIndex = Selection.getSelectionEnd(editable);  
    41.             String str = editable.toString();  
    42.             //截取新字符串  
    43.             String newStr = str.substring(0,maxLen);  
    44.             editText.setText(newStr);  
    45.             editable = editText.getText();  
    46.               
    47.             //新字符串的长度  
    48.             int newLen = editable.length();  
    49.             //旧光标位置超过字符串长度  
    50.             if(selEndIndex > newLen)  
    51.             {  
    52.                 selEndIndex = editable.length();  
    53.             }  
    54.             //设置新光标所在的位置  
    55.             Selection.setSelection(editable, selEndIndex);  
    56.               
    57.         }  
    58.     }  
    59.   
    60. }  


    对应的 activity 部分的调用为:

    [java] view plaincopy
     
    1. package cie.textEdit;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.text.InputFilter;  
    6. import android.widget.EditText;  
    7.   
    8. public class TextEditActivity extends Activity {  
    9.     /** Called when the activity is first created. */  
    10.     @Override  
    11.     public void onCreate(Bundle savedInstanceState) {  
    12.         super.onCreate(savedInstanceState);  
    13.         setContentView(R.layout.main);  
    14.   
    15.         EditText editText = (EditText) findViewById(R.id.entry);  
    16.         editText.addTextChangedListener(new MaxLengthWatcher(10, editText));  
    17.   
    18.     }  
    19. }  

    限制输入字符数为10个

    main.xml 文件

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent">  
    5.     <TextView  
    6.         android:id="@+id/label"  
    7.         android:layout_width="fill_parent"  
    8.         android:layout_height="wrap_content"  
    9.         android:text="Type here:"/>  
    10.     <EditText  
    11.         android:id="@+id/entry"  
    12.         android:singleLine="true"  
    13.         android:layout_width="fill_parent"  
    14.         android:layout_height="wrap_content"  
    15.         android:background="@android:drawable/editbox_background"  
    16.         android:layout_below="@id/label"/>  
    17.     <Button  
    18.         android:id="@+id/ok"  
    19.         android:layout_width="wrap_content"  
    20.         android:layout_height="wrap_content"  
    21.         android:layout_below="@id/entry"  
    22.         android:layout_alignParentRight="true"  
    23.         android:layout_marginLeft="10dip"  
    24.         android:text="OK" />  
    25.     <Button  
    26.         android:layout_width="wrap_content"  
    27.         android:layout_height="wrap_content"  
    28.         android:layout_toLeftOf="@id/ok"  
    29.         android:layout_alignTop="@id/ok"  
    30.         android:text="Cancel" />  
    31. </RelativeLayout>  


    效果为输入了10个字符后,光标停在末尾



  • 相关阅读:
    Spring Boot----freemark使用
    vue----解决跨域问题
    CSS----精灵图
    Spring MVC----文件上传
    Mybatis----Mybatis使用
    Django url引用异常
    Django ORM异常
    django sqlite3 报错问题
    爬虫 requests的超时和重试
    python 异常 NameError: name 'ModuleNotFoundError' is not defined
  • 原文地址:https://www.cnblogs.com/wi100sh/p/4482537.html
Copyright © 2011-2022 走看看