EditText限制字符串输入长度的改动办法:
1、在 xml 文件中设置文本编辑框属性作字符数限制
如:android:maxLength="10" 即限制最大输入字符个数为10
2、在代码中使用InputFilter 进行过滤
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20
也可在InputFilter里添加新方法
3、利用 TextWatcher 进行监听
在对应的Activity中调用TextWather
public static class AdnNameLengthFilter implements InputFilter根据输入字符串或者显示字符串的字节长度和字符串长度判断,因为汉字在android中一个字符占3个字节,EditText输入最长为32个字节所以汉字只能输入10个字符。
{
private int nMax;
public CharSequence filter (CharSequence source, int start, int end, Spanned dest, int dstart, int dend){
if((dest.toString().getBytes(Charset.defaultCharset()).length <= 32 && dest.toString().length() > 10)
|| (source.toString().getBytes(Charset.defaultCharset()).length <= 32 && source.toString().length() > 10)){
nMax = 30;
}else{
nMax = 10;
}
int keep = nMax - (dest.length() - (dend - dstart));
if (keep <= 0) {
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
keep += start;
if (Character.isHighSurrogate(source.charAt(keep - 1))) {
--keep;
if (keep == start) {
return "";
}
}
return source.subSequence(start, start + keep);
}
}
}
3、利用 TextWatcher 进行监听
package com.example.test12;import android.os.Bundle;import android.R.menu;import android.app.Activity;import android.text.Editable;import android.text.TextWatcher;import android.util.Log;import android.view.Menu;import android.widget.EditText;public class MainActivity extends Activity implements TextWatcher{ EditText mEditText; public static final int MAX_BYTE_LENGTH=30; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText=(EditText) findViewById(R.id.edittext); mEditText.addTextChangedListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub String text=mEditText.getText().toString(); int inputByteLength=0; for(int i=0;i<text.length();i++){ inputByteLength+=text.substring(i, i+1).getBytes().length; } int beforByteLength=0; String beforString = s.toString(); for(int i=0;i<beforString.length();i++){ beforByteLength+=beforString.substring(i, i+1).getBytes().length; } if(inputByteLength > MAX_BYTE_LENGTH){ mEditText.setText(s); mEditText.setSelection(mEditText.getText().toString().length()); return; } Log.d("TXX","beforeTextChanged-S:"+s); Log.d("TXX","beforeTextChanged-start:"+start); Log.d("TXX","beforeTextChanged-count:"+count); Log.d("TXX","beforeTextChanged-after:"+after); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String text=mEditText.getText().toString(); int maxStringLength=0; int inputByteLength=0; for(int i=0;i<text.length();i++){ inputByteLength+=text.substring(i, i+1).getBytes().length; if(inputByteLength > MAX_BYTE_LENGTH){ maxStringLength=i; } } if(inputByteLength > MAX_BYTE_LENGTH){ mEditText.setText(s.subSequence(0, maxStringLength)); mEditText.setSelection(mEditText.getText().toString().length()); return; } }}输入的字符串限制最大为30个字节,遍历输入的字符串字符得出字节长度如果大于MAX_BYTE_LENGTH最大字节数,则返回从0到i的字符串。
在对应的Activity中调用TextWather
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));
}
}