EditText 对手机号码的处理

@Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        hideRechargeBar();
        Intent intent = getIntent();
        String state = intent.getStringExtra("state");

        if (state.equals("forget")) {
            timeCount2 = new TimeCount2(60000, 1000);
            setTopTitle(R.string.textView_forget);
            setContentView(R.layout.activity_forget);
            initForget();
        } else if (state.equals("register")) {
            timeCount = new TimeCount(60000, 1000);
            setTopTitle(R.string.textView_regist);
            setContentView(R.layout.activity_regist);
            initRegister();
        }
        mHttpService = new HttpService(RegistActivity.this);
    }
// 判断处理的是账号注册
private final static int REGISTER_USER_PHONE = 1;
// 判断处理的是忘记密码
private final static int FORGET_USER_NUMBER = 2;
// 获取用户输入的手机号码
private String getFinalPhoneNumber;
/**
     * HF add
     * 账号注册 - 识别手机号码,格式化手机号码,自动空格
     * 忘记密码 - 识别手机号码,格式化手机号码,自动空格
     */
    private TextWatcher chooseTextWatcher(final int type) {

        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

                if (count == 1) {
                    if (s.length() == 4) {
                        String text = s.subSequence(0, s.length() - 1) + " " + s.subSequence(s.length() - 1, s.length());

                        switch (type) {
                            case REGISTER_USER_PHONE:
                                registerUserPhone.setText(text);
                                registerUserPhone.setSelection(5);
                                break;
                            case FORGET_USER_NUMBER:
                                forgetUserNumber.setText(text);
                                forgetUserNumber.setSelection(5);
                                break;
                        }
                    }

                    if (s.length() == 9) {
                        String text = s.subSequence(0, s.length() - 1) + " " + s.subSequence(s.length() - 1, s.length());

                        switch (type) {
                            case REGISTER_USER_PHONE:
                                registerUserPhone.setText(text);
                                registerUserPhone.setSelection(10);
                                break;
                            case FORGET_USER_NUMBER:
                                forgetUserNumber.setText(text);
                                forgetUserNumber.setSelection(10);
                                break;
                        }
                    }
                } else if (count == 0) {
                    if (s.length() == 4) {
                        switch (type) {
                            case REGISTER_USER_PHONE:
                                registerUserPhone.setText(s.subSequence(0, s.length() - 1));
                                registerUserPhone.setSelection(3);
                                break;
                            case FORGET_USER_NUMBER:
                                forgetUserNumber.setText(s.subSequence(0, s.length() - 1));
                                forgetUserNumber.setSelection(3);
                                break;
                        }
                    }
                    if (s.length() == 9) {
                        switch (type) {
                            case REGISTER_USER_PHONE:
                                registerUserPhone.setText(s.subSequence(0, s.length() - 1));
                                registerUserPhone.setSelection(8);
                                break;
                            case FORGET_USER_NUMBER:
                                forgetUserNumber.setText(s.subSequence(0, s.length() - 1));
                                forgetUserNumber.setSelection(8);
                                break;
                        }
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                switch (type) {
                    case REGISTER_USER_PHONE:
                        getFinalPhoneNumber = registerUserPhone.getText().toString().replaceAll("\\s+", "");
                        break;
                    case FORGET_USER_NUMBER:
                        getFinalPhoneNumber = forgetUserNumber.getText().toString().replaceAll("\\s+", "");
                        break;
                }
                int numLength = getFinalPhoneNumber.length();
                if (numLength > 11) {
                    Toast showToast = Toast.makeText(RegistActivity.this, "号码长度有误,请输入11位正确号码", Toast.LENGTH_LONG);
                    showToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    showToast.show();
                }
            }

        };
    }

    /**
     * HF add,对 EditText 的焦点进行处理,转移焦点后确定将手机号码格式化成 xxx-xxxx-xxxx 的形式
     *
     * @param type 判断处理的页面类型,账号注册 或 忘记密码
     * @return
     */
    private View.OnFocusChangeListener chooseFocusChangeListener(final int type) {
        return new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    // TODO nothing
                } else {
                    int phoneLength = getFinalPhoneNumber.replaceAll("\\s+", "").length();
                    // 判断一下手机号码是否合理,并将手机号码格式化成 xxx-xxxx-xxxx 的形式
                    if (phoneLength == 11) {
                        String formatPhoneNumber = getFinalPhoneNumber.substring(0, 3) + " " + getFinalPhoneNumber.substring(3, 7) + " " + getFinalPhoneNumber.substring(7);
                        switch (type) {
                            case REGISTER_USER_PHONE:
                                registerUserPhone.setText(formatPhoneNumber);
                                break;
                            case FORGET_USER_NUMBER:
                                forgetUserNumber.setText(formatPhoneNumber);
                                break;
                        }
                    } else {
                        Toast showToast = Toast.makeText(RegistActivity.this, "号码长度有误,请输入11位正确号码", Toast.LENGTH_LONG);
                        showToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        showToast.show();
                    }
                }
            }
        };
    }

    //注册
    private void initRegister() {
        registerUserPhone = (EditText) findViewById(R.id.register_user_phone);
        registerUserPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        /* HF add start */
        registerUserPhone.addTextChangedListener(chooseTextWatcher(REGISTER_USER_PHONE));
        //registerUserPhone.setFilters(new InputFilter[]{new InputFilter.LengthFilter(13)});
        registerUserPhone.setOnFocusChangeListener(chooseFocusChangeListener(REGISTER_USER_PHONE));
        /* HF add end */
        verificationCodeBtn = (Button) findViewById(R.id.register_gain_verificationCode);
        verificationCodeBtn.setOnClickListener(this);
        registerVerificationCode = (EditText) findViewById(R.id.register_verification_code);
        registerUserPassWord = (EditText) findViewById(R.id.register_user_password);
        registerAffirmUserPassWord = (EditText) findViewById(R.id.register_user_affirm_password);
        personalAccount = (Button) findViewById(R.id.personal_account);
        personalAccount.setOnClickListener(this);
        groupAccount = (Button) findViewById(R.id.group_account);
        groupAccount.setOnClickListener(this);
        userCheckbox = (CheckBox) findViewById(R.id.user_checkbox);
        registerCommit = (Button) findViewById(R.id.register_commit);
        registerCommit.setOnClickListener(this);
        doermiProtocol = (TextView) findViewById(R.id.doermi_protocol);
        doermiProtocol.setOnClickListener(this);

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值