0
点赞
收藏
分享

微信扫一扫

android editText 字符长度限制

爱做梦的老巫婆 2022-03-30 阅读 70


方法一:

在 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. EditText editText = (EditText)findViewById(R.id.entry);
  8. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
  9. }
  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. import android.text.Editable;
  3. import android.text.Selection;
  4. import android.text.TextWatcher;
  5. import android.widget.EditText;
  6. /*
  7. * 监听输入内容是否超出最大长度,并设置光标位置
  8. * */
  9. publicclass MaxLengthWatcher implements TextWatcher {
  10. privateint maxLen = 0;
  11. private EditText editText = null;
  12. public MaxLengthWatcher(int maxLen, EditText editText) {
  13. this.maxLen = maxLen;
  14. this.editText = editText;
  15. }
  16. publicvoid afterTextChanged(Editable arg0) {
  17. // TODO Auto-generated method stub
  18. }
  19. publicvoid beforeTextChanged(CharSequence arg0, int arg1, int arg2,
  20. int arg3) {
  21. // TODO Auto-generated method stub
  22. }
  23. publicvoid onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  24. // TODO Auto-generated method stub
  25. Editable editable = editText.getText();
  26. int len = editable.length();
  27. if(len > maxLen)
  28. {
  29. int selEndIndex = Selection.getSelectionEnd(editable);
  30. String str = editable.toString();
  31. //截取新字符串
  32. String newStr = str.substring(0,maxLen);
  33. editText.setText(newStr);
  34. editable = editText.getText();
  35. //新字符串的长度
  36. int newLen = editable.length();
  37. //旧光标位置超过字符串长度
  38. if(selEndIndex > newLen)
  39. {
  40. selEndIndex = editable.length();
  41. }
  42. //设置新光标所在的位置
  43. Selection.setSelection(editable, selEndIndex);
  44. }
  45. }
  46. }


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. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.text.InputFilter;
  5. import android.widget.EditText;
  6. publicclass TextEditActivity extends Activity {
  7. /** Called when the activity is first created. */
  8. @Override
  9. publicvoid onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. EditText editText = (EditText) findViewById(R.id.entry);
  13. editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
  14. }
  15. }


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 条评论