1.原理说明,中文占用2个字符,以此来过滤
一般在监听输入可以使用TextWatcher这个监视器,但是动态输入还使用它的话就会出现bug,因为Watcher观察者的3个函数都不是那么好使用,使得输入缓冲区有中文不会显示在输入框中,即使使用截断字符创的方式来控制显示,但是如果输入了一串英文字符后,再将光标定位在英文字符串中进行输入中文,bug就出现了。这个时候就必须使用filter来过滤了。
废话少说,看代码:
情景:如果有中文字符的话,输入框只能输入固定字符比如说6个,如果全英文字符,可以输入20个,如果超过6个字符,想输入中文的话,就将中文过滤。
package cn.edu.wtu;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.widget.EditText;
public class InputFilterActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "InputFilterActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edit = (EditText)findViewById(R.id.editText1);
edit.setText("moon");
edit.setFilters(new InputFilter[]{
new InputFilter.LengthFilter(20),
new InputFilter(){
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
// TODO Auto-generated method stub
Log.d(TAG, source.toString()+"\tstart:"+start+"\tend:"+end);
Log.d(TAG, dest.toString()+"\tdstart:"+dstart+"\tdend:"+dend);
if(isCN(source.toString())){
if(dstart >= 6){
edit.setSelection(dstart);
return "";
}else{
if(source.length()+dest.length() > 6){
return source.subSequence(start, start+6-dest.length());
}else{
return source;
}
}
}else{
return source;
}
}
}
});
}
public boolean isCN(String str){
try {
byte [] bytes = str.getBytes("UTF-8");
if(bytes.length == str.length()){
return false;
}else{
return true;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
xml文件就不贴了,很简单,因为中文的UNICODE编码是2个字节,所以可以利用这个特性判断是否为中文字符
原文链接http://blog.csdn.net/fuuckwtu/article/details/6799904
2.利用IntputFilter
public static void inputFilterSpace(final EditText edit){
edit.setFilters(new InputFilter[]
{
new InputFilter.LengthFilter(16),
new InputFilter(){
public CharSequence filter(CharSequence src, int start, int end, Spanned dst, int dstart, int dend) {
Log.d(TAG,"src:"+src+";start:"+start+";end:"+end);
Log.d(TAG,"dest:"+dst+";dstart:"+dstart+";dend:"+dend);
if(src.length()<1)
{
return null;
}
else
{
char temp [] = (src.toString()).toCharArray();
char result [] = new char[temp.length];
for(int i = 0,j=0; i< temp.length; i++){
if(temp[i] == ' '){
continue;
}else{
result[j++] = temp[i];
}
}
return String.valueOf(result).trim();
}
}
}
});
}
原文链接:http://blog.csdn.net/fuuckwtu/article/details/6968766
3.android同时控制EditText输入字符个数和禁止特殊字符输入的方法,涉及Android操作EditText控制字符操作的技巧
int
mMaxLenth =
200
;
//设置允许输入的字符长度
public
static
String stringFilter(String str)
throws
PatternSyntaxException{
String regEx =
"[/\\:*?<>|\"\n\t]"
;
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return
m.replaceAll(
""
);
}
mEditText.addTextChangedListener(
new
TextWatcher() {
private
int
cou =
0
;
int
selectionEnd =
0
;
@Override
public
void
onTextChanged(CharSequence s,
int
start,
int
before,
int
count) {
cou = before + count;
String editable = mEditText.getText().toString();
String str = stringFilter(editable);
//过滤特殊字符
if
(!editable.equals(str)) {
mEditText.setText(str);
}
mEditText.setSelection(mEditText.length());
cou = mEditText.length();
}
@Override
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
int
after) {
}
@Override
public
void
afterTextChanged(Editable s) {
if
(cou > mMaxLenth) {
selectionEnd = mEditText.getSelectionEnd();
s.delete(mMaxLenth, selectionEnd);
}
}
});
总结:InputFilter需要重点了解下,用例范围应该很广,如有更好的办法,还希望各位博友多多指教!