导航:首页 > 操作系统 > android限制输入字符

android限制输入字符

发布时间:2022-10-29 11:15:44

1. android 中如何限制 EditText 最大输入字符数

方法一:

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

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

方法二:

在代码中使用InputFilter 进行过滤

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

[java] view plainprint?
01.public class TextEditActivity extends Activity {
02. /** Called when the activity is first created. */
03. @Override
04. public void onCreate(Bundle savedInstanceState) {
05. super.onCreate(savedInstanceState);
06. setContentView(R.layout.main);
07.
08. EditText editText = (EditText)findViewById(R.id.entry);
09. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
10. }
11.}
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 plainprint?
01.package cie.textEdit;
02.
03.import android.text.Editable;
04.import android.text.Selection;
05.import android.text.TextWatcher;
06.import android.widget.EditText;
07.
08./*
09. * 监听输入内容是否超出最大长度,并设置光标位置
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.}
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 plainprint?
01.package cie.textEdit;
02.
03.import android.app.Activity;
04.import android.os.Bundle;
05.import android.text.InputFilter;
06.import android.widget.EditText;
07.
08.public class TextEditActivity extends Activity {
09. /** 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.}
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 plainprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="fill_parent"
04. android:layout_height="fill_parent">
05. <TextView
06. android:id="@+id/label"
07. android:layout_width="fill_parent"
08. android:layout_height="wrap_content"
09. 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>

2. android 中如何限制 EditText 最大输入字符数

方法一:

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

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

方法二:

在代码中使用InputFilter 进行过滤

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
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 进行监听


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 部分的调用为:

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));

}
}


main.xml 文件

[html] view plainprint?
01.<?xml version="1.0" encoding="utf-8"?>
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="fill_parent"
04. android:layout_height="fill_parent">
05. <TextView
06. android:id="@+id/label"
07. android:layout_width="fill_parent"
08. android:layout_height="wrap_content"
09. 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>

3. android 中如何限制 EditText 最大输入字符数

方法一:
在 xml 文件中设置文本编辑框属性作字符数限制
如:Android:maxLength="10" 即限制最大输入字符个数为10
在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20

[java] view plain
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)});
}
}

4. android 中如何限制 EditText 最大输入字符数

方法1::在布局文件中设置文本编辑框属性作字符数限制,android:maxLength="10" 即限制最大输入字符个数为10


方法2:在代码中使用InputFilter 进行过滤

{
privateEditTexttext;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text=(EditText)findViewById(R.id.text);
text.setFilters(newInputFilter[]{newInputFilter.LengthFilter(20)});
}
}


方法3:利用 TextWatcher 进行监听

继承TextWatcher接口,对输入进行监听

{

privateintmaxLen=0;
privateEditTexteditText=null;


publicMaxLengthWatcher(intmaxLen,EditTexteditText){
this.maxLen=maxLen;
this.editText=editText;
}


publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){
//TODOAuto-generatedmethodstub
Editableeditable=editText.getText();
intlen=editable.length();

if(len>maxLen)
{
intselEndIndex=Selection.getSelectionEnd(editable);
Stringstr=editable.toString();
//截取新字符串
StringnewStr=str.substring(0,maxLen);
editText.setText(newStr);
editable=editText.getText();

//新字符串的长度
intnewLen=editable.length();
//旧光标位置超过字符串长度
if(selEndIndex>newLen)
{
selEndIndex=editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable,selEndIndex);

}
}


@Override
publicvoidbeforeTextChanged(CharSequences,intstart,intcount,
intafter){
}


@Override
publicvoidafterTextChanged(Editables){
}

}


在activity中为EditText添加监听

{
privateEditTexttext;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text=(EditText)findViewById(R.id.entry);
//限制为10
text.addTextChangedListener(newMaxLengthWatcher(10,editText));

}
}

5. Android 系统搜索框 如何限制输入字数长度

android 搜索框就是一个EditText输入控件,或者是EditText的子类

长度限制方式有以下几种:

方法一:

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

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


方法二:

在代码中使用InputFilter 进行过滤

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

示例代码如下:

{
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

EditTexteditText=(EditText)findViewById(R.id.entry);
editText.setFilters(newInputFilter[]{newInputFilter.LengthFilter(20)});
}
}

方法三:

利用 TextWatcher 进行限制,TextWatcher是注册一个内存输入的改变事件,当你的输入框输入字符和删除字符都会触发

实现代码如下:

packagecie.textEdit;

importandroid.text.Editable;
importandroid.text.Selection;
importandroid.text.TextWatcher;
importandroid.widget.EditText;

/*
*监听输入内容是否超出最大长度,并设置光标位置
**/
{

privateintmaxLen=0;
privateEditTexteditText=null;


publicMaxLengthWatcher(intmaxLen,EditTexteditText){
this.maxLen=maxLen;
this.editText=editText;
}

publicvoidafterTextChanged(Editablearg0){
//TODOAuto-generatedmethodstub

}

publicvoidbeforeTextChanged(CharSequencearg0,intarg1,intarg2,
intarg3){
//TODOAuto-generatedmethodstub

}

publicvoidonTextChanged(CharSequencearg0,intarg1,intarg2,intarg3){
//TODOAuto-generatedmethodstub
Editableeditable=editText.getText();
intlen=editable.length();

if(len>maxLen)
{
intselEndIndex=Selection.getSelectionEnd(editable);
Stringstr=editable.toString();
//截取新字符串
StringnewStr=str.substring(0,maxLen);
editText.setText(newStr);
editable=editText.getText();

//新字符串的长度
intnewLen=editable.length();
//旧光标位置超过字符串长度
if(selEndIndex>newLen)
{
selEndIndex=editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable,selEndIndex);

}
}

}

有关EditText 即Android输入框的更多用法,建议查看官网API文档

6. android 中如何限制 EditText 最大输入字符数

android 中限制 EditText 最大输入字符数可以在 xml 文件中设置文本编辑框属性作字符数限制,操作步骤如下:

1、首先使用Android studio创建一个android工程,工程视图如下:

7. android 中如何限制 EditText 最大输入字符数

在windows7开发平台,android studio开发工具使用如下办法用来限制EditText 最大输入字符数。

1、首先在打开Layout文件,如下图:

阅读全文

与android限制输入字符相关的资料

热点内容
脚本提取源码器 浏览:928
smo源码 浏览:875
为什么要搭建单独服务器 浏览:478
编译器有什么控制 浏览:891
希尔伯特pdf 浏览:645
php数组全数字 浏览:645
解密塔罗牌小程序源码 浏览:862
聚合跑分源码 浏览:553
注册dns服务器写什么 浏览:879
linux安装deb包 浏览:521
电脑盘文件夹如何平铺 浏览:267
相机卡满了没文件夹 浏览:751
如何批量快速压缩视频 浏览:432
我的世界如何加入ice服务器 浏览:873
兄弟cnc编程说明书 浏览:204
php闪电入门教程学习 浏览:152
金岳霖逻辑pdf 浏览:938
linuxtomcat线程 浏览:77
pboc长度加数据加密 浏览:187
英雄联盟国际服手游怎么下安卓 浏览:298