android editText 字符长度限制

方法一:

在 xml 文件中设置文本编辑框属性作字符数限制

如:android:maxLength="10" 即限制最大输入字符个数为10

 

方法二:

在代码中使用InputFilter 进行过滤

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20

 

 

  1. publicclass TextEditActivity extends Activity { 
  2.     /** Called when the activity is first created. */ 
  3.     @Override 
  4.     publicvoid 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.     } 
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 进行监听

 

  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. publicclass MaxLengthWatcher implements TextWatcher { 
  12.  
  13.     privateint 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.     publicvoid afterTextChanged(Editable arg0) { 
  23.         // TODO Auto-generated method stub 
  24.          
  25.     } 
  26.  
  27.     publicvoid beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
  28.             int arg3) { 
  29.         // TODO Auto-generated method stub 
  30.          
  31.     } 
  32.  
  33.     publicvoid 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.  
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 部分的调用为:

 

 

  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. publicclass TextEditActivity extends Activity { 
  9.     /** Called when the activity is first created. */ 
  10.     @Override 
  11.     publicvoid 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.     } 
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 文件

 

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <RelativeLayoutxmlns: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> 
<?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个字符后,光标停在末尾


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

generallizhong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值