android 中如何限制 EditText 最大输入字符数
来源:互联网 发布:js选项卡制作 编辑:程序博客网 时间:2024/05/22 00:14
方法一:
在 xml 文件中设置文本编辑框属性作字符数限制
如:android:maxLength="10" 即限制最大输入字符个数为10
方法二:
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
- publicclass TextEditActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- publicvoid 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)});
- }
- }
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 进行监听
- package cie.textEdit;
- import android.text.Editable;
- import android.text.Selection;
- import android.text.TextWatcher;
- import android.widget.EditText;
- /*
- * 监听输入内容是否超出最大长度,并设置光标位置
- * */
- publicclass MaxLengthWatcher implements TextWatcher {
- privateint maxLen = 0;
- private EditText editText = null;
- public MaxLengthWatcher(int maxLen, EditText editText) {
- this.maxLen = maxLen;
- this.editText = editText;
- }
- publicvoid afterTextChanged(Editable arg0) {
- // TODO Auto-generated method stub
- }
- publicvoid beforeTextChanged(CharSequence arg0, int arg1, int arg2,
- int arg3) {
- // TODO Auto-generated method stub
- }
- publicvoid 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);
- }
- }
- }
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 stubEditable 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 部分的调用为:
- package cie.textEdit;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.InputFilter;
- import android.widget.EditText;
- publicclass TextEditActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- EditText editText = (EditText) findViewById(R.id.entry);
- editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
- }
- }
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 文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns: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>
<?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 0
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数(2)
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android 中如何限制 EditText 最大输入字符数
- android开发(十六) 如何限制 EditText 最大输入字符数
- Android-02 如何限制 EditText 最大输入字符数
- Java 8新特性终极指南
- java 简单的jdbc连接数据库代码示例
- PowerVR 7系列GPU
- 《OpenCV学习笔记》---第一篇 第一个程序
- 使用技巧Windows
- android 中如何限制 EditText 最大输入字符数
- android官方SearchView详细使用
- 常用的json解析方式
- 成功的背后
- iOS开发——Masonry 自动布局
- sql server2008本地连接选择windows身份验证无法登陆的解决办法
- linux 常用命令
- php发送get、post请求的几种方法
- js ==与===区别(两个等号与三个等号)