您的位置:首页 > 其它

设置EditText只能输入字母,数字和英文字符

2017-08-30 16:31 483 查看
package com.showsoft.view;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

import com.showsoft.utils.L;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by Administrator on 2017/8/30.
*/

public abstract class PasswordTextWatcher implements TextWatcher {
private static final String LOG_TAG = "wanlijun";
private boolean mIsMatch;

private CharSequence mResult;

private int mSelectionStart;

private int mSelectionEnd;

private EditText mPswEditText;
public PasswordTextWatcher() {};

public PasswordTextWatcher(EditText editText) {

mPswEditText = editText;

};
@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

// TODO Auto-generated method stub
L.v(LOG_TAG, "onTextChanged---> s = " + s + "; start = " + start

+ "; before = " + before + "; count = " + count);

L.v(LOG_TAG,

"onTextChanged --- > SelectionStart = "
+ mPswEditText.getSelectionStart()

+ "; SelectionEnd = " + mPswEditText.getSelectionEnd());

L.v(LOG_TAG, "getText = " + mPswEditText.getText() + "; mSelectionStart = " + mSelectionStart);

CharSequence charSequence = "";

if ((mSelectionStart + count) <= s.length()) {

charSequence = s.subSequence(mSelectionStart, mSelectionStart + count);

}

L.v(LOG_TAG, "charSequence = " + charSequence);

L.v(LOG_TAG, "isMatch = " + mIsMatch);

if (!mIsMatch) {

mIsMatch = pswFilter(charSequence);

String temp = s.toString();

L.v(LOG_TAG, "temp = " + temp);

mResult = temp.replace(charSequence, "");

L.d(LOG_TAG, "onTextChanged---> mSelectionEnd = " + mSelectionEnd);

mSelectionEnd = start;

L.v(LOG_TAG, "result = " + mResult);

}

}

@Override

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

// TODO Auto-generated method stub
L.v(LOG_TAG, "beforeTextChanged---> s = " + s + "; start = " + start

+ "; after = " + after + "; count = " + count);

L.v(LOG_TAG,

"beforeTextChanged --- > SelectionStart = "
+ mPswEditText.getSelectionStart()

+ "; SelectionEnd = " + mPswEditText.getSelectionEnd());

mSelectionStart = mPswEditText.getSelectionStart();

}

@Override

public void afterTextChanged(Editable s) {

// TODO Auto-generated method stub
L.v(LOG_TAG, "afterTextChanged ---> s = " + s + "; mIsMatch = " + mIsMatch);

if (!mIsMatch) {

mIsMatch = true;

mPswEditText.setText(mResult);

L.v(LOG_TAG, "--- befor setSelection --- result = " + mResult);

L.d(LOG_TAG, "afterTextChanged---> mSelectionEnd = " + mSelectionEnd);

mPswEditText.setSelection(mSelectionEnd);

L.v(LOG_TAG, "--- after setText --- result = " + mResult);

}
mIsMatch = false;

L.v(LOG_TAG, "-------------------------------------mSelectionStart = " + mSelectionStart);

}

private boolean pswFilter(CharSequence s) {

if (TextUtils.isEmpty(s)) {

return true;

}

//String regex = "[A-Z0-9a-z!@#$%^&*.~///{//}|()'/\"?><,.`//+-=_//[//]:;]+";
String regex = "[A-Z0-9a-z]+";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(s);

if (matcher.matches()) {

return true;

}

return false;

}
}
原文章更详细:http://www.th7.cn/Program/Android/201505/459831.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: