设置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
相关文章推荐
- Android设置EditText只能输入字母和数字
- android edittext只能输入字母和数字,默认弹出英文输入法 。
- android edittext只能输入字母和数字,默认弹出英文输入法
- EditText设置输入的类型,比如说限制只能输入字母和数字
- 设置EditText只能输入数字和字母
- 设置EditText只能输入字母和数字
- Android EditText输入限制最大字符长度 和 限制只能输入数字和字母(包含大小写)
- Android editview 设置只能输入数字、字母和汉字
- android代码设置EditText只输入数字、字母
- 设置EditText只能输入一些特定的字符
- JavaScript 中怎样判断文本框只能输出英文字母、汉字和数字,不能输入特殊字符!
- Android EditText 限制输入数字和字母设置
- android 中怎么控制EditText只能输入数字和字母?
- EditText自定义输入数据格式,比如只能是数字和字母(身份证最后一位X)
- Android EditText设置只能输入数字和小数点
- edittext设置只能输入数字
- android 设置EditText只能输入数字
- Android学习笔记——设置EditText只输入数字、字母
- android的edittext设置输入限制,只能输入数字
- Edittext属性(2)之控制输入类型只能为数字和字母