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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值