導航:首頁 > 操作系統 > 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限制輸入字元相關的資料

熱點內容
pboc長度加數據加密 瀏覽:187
英雄聯盟國際服手游怎麼下安卓 瀏覽:297
程序員的思路 瀏覽:234
只能用命令獲得的四種方塊 瀏覽:358
怎麼用命令方塊防止開創造 瀏覽:807
掃描版的pdf 瀏覽:790
編程貓怎樣做3d游戲 瀏覽:207
怎麼查找雲伺服器上的ftp 瀏覽:156
我的世界伺服器如何注冊賬號 瀏覽:934
統計英文字元python 瀏覽:423
linux信息安全 瀏覽:908
壓縮機接線柱爆 瀏覽:999
程序員自主創業 瀏覽:584
匯編程序員待遇 瀏覽:359
怎麼批量有順序的命名文件夾 瀏覽:211
杭州程序員健身 瀏覽:20
dvd光碟存儲漢子演算法 瀏覽:758
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145