登录,注册,修改密码,倒计时工具类编写

废话不多说,直接上代码:

   计时器工具类:继承安卓原生CountDownTimer 类!

public class CountDownTimerUtils extends CountDownTimer {
    private TextView mTextView;

    /**
     * @param textView          The TextView
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receiver
     *                          {@link #onTick(long)} callbacks.
     */
    public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false); //设置不可点击
        mTextView.setText(  "重新发送("+millisUntilFinished / 1000+")");  //设置倒计时时间
        mTextView.setBackgroundResource(R.color.dark_gray); //设置按钮为灰色,这时是不能点击的

        /**
         * 超链接 URLSpan
         * 文字背景颜色 BackgroundColorSpan
         * 文字颜色 ForegroundColorSpan
         * 字体大小 AbsoluteSizeSpan
         * 粗体、斜体 StyleSpan
         * 删除线 StrikethroughSpan
         * 下划线 UnderlineSpan
         * 图片 ImageSpan
         * http://blog.csdn.net/ah200614435/article/details/7914459
         */
        SpannableString spannableString = new SpannableString(mTextView.getText().toString());  //获取按钮上的文字
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        int length = mTextView.getText().toString().trim().length();
        /**
         * public void setSpan(Object what, int start, int end, int flags) {
         * 主要是start跟end,start是起始位置,无论中英文,都算一个。
         * 从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。
         */
        spannableString.setSpan(span, 5, length-1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
        mTextView.setText(spannableString);
    }

    @Override
    public void onFinish() {
        mTextView.setText("重新获取");
        mTextView.setClickable(true);//重新获得点击
        mTextView.setBackgroundResource(R.color.blue_green);  //还原背景色
    }
}

xml中代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <ImageView
        android:background="@color/blue_tog_btn_pressed"
        android:id="@+id/bt_return"
        android:layout_width="@dimen/dp_48"
        android:layout_height="@dimen/dp_48"
        app:srcCompat="@drawable/vector_drawable_return" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="@dimen/dp_28"
        android:paddingRight="@dimen/dp_28">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="100dp" />

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@null"
            android:drawableLeft="@drawable/general_ic_email"
            android:drawablePadding="@dimen/dp_10"
            android:hint="@string/username_reset"
            android:singleLine="true" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.6dp"
            android:layout_marginTop="5dp"
            android:background="@color/black" />
        <!--验证码-->
        <LinearLayout
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_48">

            <EditText
                android:id="@+id/verification_code"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="3"
                android:background="@null"
                android:gravity="center"
                android:hint="@string/receive_code"
                android:imeOptions="actionDone"
                android:singleLine="true" />

            <TextView
                android:id="@+id/phone_verification_code"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:background="@color/dark_blue"
                android:gravity="center"
                android:text="验证码"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_18" />
        </LinearLayout>


        <View
            android:layout_width="match_parent"
            android:layout_height="0.6dp"
            android:background="@color/black" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_48">

            <EditText
                android:id="@+id/pwd"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:background="@null"
                android:drawableLeft="@drawable/general_ic_password"
                android:drawablePadding="@dimen/dp_10"
                android:hint="@string/pwd_new"
                android:imeOptions="actionDone"
                android:inputType="textPassword"
                android:singleLine="true" />

            <ImageView
                android:id="@+id/image_pwd"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:src="@drawable/general_password_hidden" />
        </RelativeLayout>


        <View
            android:layout_width="match_parent"
            android:layout_height="0.6dp"
            android:background="@color/black" />

        <!--再次确认密码-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_48">

            <EditText

                android:id="@+id/pwd_again"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:background="@null"
                android:drawableLeft="@drawable/general_ic_password"
                android:drawablePadding="@dimen/dp_10"
                android:hint="@string/pwd_again"
                android:imeOptions="actionDone"
                android:inputType="textPassword"
                android:singleLine="true" />

            <ImageView
                android:id="@+id/image_pwd2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:src="@drawable/general_password_hidden" />
        </RelativeLayout>


        <View
            android:layout_width="match_parent"
            android:layout_height="0.6dp"
            android:background="@color/black" />


        <Button

            android:id="@+id/pwd_bt_reset"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_48"
            android:layout_marginTop="@dimen/dp_100"
            android:background="@drawable/bg_btn_sure"
            android:text="@string/pwd_reset" />
    </LinearLayout>
</LinearLayout>




activity中代码:

public class ForgetPasswordActivity extends NewBaseActivity {

    private static final String TAG = "ForgetPasswordActivity";
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.verification_code)
    EditText verificationCode;
    @BindView(R.id.phone_verification_code)
    TextView phoneVerificationCode;
    @BindView(R.id.pwd)
    EditText et_pwd;
    @BindView(R.id.image_pwd)
    ImageView imagePwd;
    @BindView(R.id.pwd_again)
    EditText pwdAgain;
    @BindView(R.id.image_pwd2)
    ImageView imagePwd2;
    @BindView(R.id.pwd_bt_reset)
    Button pwdBtReset;
    private boolean isopen=true;//用来标记密码是否可见
    private boolean isopen2=true;//用来标记密码是否可见
    private   String phone;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initComplete(Bundle savedInstanceState) {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        setOnListener(new UpdateUIVericationCode() {
            @Override
            public void setupdateUIVericationCode() {
                upDatePhoneVerificationCodeUI();
            }
        });
    }

    @OnClick({R.id.bt_return, R.id.pwd_bt_reset,R.id.image_pwd, R.id.image_pwd2,R.id.phone_verification_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_return:
                this.finish();
                break;
            case R.id.pwd_bt_reset://重置密码
                //验证验证码
                verifacityCode();
                break;
            case R.id.image_pwd:
                if (isopen) {
                    et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    imagePwd.setImageDrawable(getResources().getDrawable(R.drawable.general_password_show));
                } else {
                    et_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    imagePwd.setImageDrawable(getResources().getDrawable(R.drawable.general_password_hidden));
                }
                isopen = !isopen;
                break;
            case R.id.image_pwd2:
                if (isopen2) {
                    pwdAgain.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    imagePwd2.setImageDrawable(getResources().getDrawable(R.drawable.general_password_show));
                } else {
                    pwdAgain.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    imagePwd2.setImageDrawable(getResources().getDrawable(R.drawable.general_password_hidden));
                }
                isopen2 = !isopen2;
                break;
            case R.id.phone_verification_code://请求验证码
                requestVerificationCode();
                break;
            default:
                break;
        }
    }

    //请求验证码
    private void requestVerificationCode() {
        phone = username.getText().toString().trim();
        if (TextUtils.isEmpty(phone)){
            ToastUtil.showShortToast(mContext,"手机号码不能为空,请重新输入!");
            return;
        }
        BmobSMS.requestSMSCode(this, phone, "register", new RequestSMSCodeListener() {
            @Override
            public void done(Integer smsId, cn.bmob.sms.exception.BmobException ex) {
                if (ex == null) {//验证码发送成功
                    Log.e("bmob", "短信id:" + smsId);//用于查询本次短信发送详情
                    myListener.setupdateUIVericationCode();
                }else{
                    Log.e(TAG, "done: -------------------------"+ex.getErrorCode()+","+ex.getMessage()+","+smsId );
                }
            }


        });


    }

    /**
     * 更新验证码UI
     */
    private void upDatePhoneVerificationCodeUI() {
        phoneVerificationCode.post(new Runnable() {
            @Override
            public void run() {
                phoneVerificationCode.setText("已发送");
                CountDownTimerUtils timer = new CountDownTimerUtils(phoneVerificationCode,60000,1000);
                timer.start();
            }
        });

    }
    //验证验证码
    private void verifacityCode() {
        String register_code = verificationCode.getText().toString().trim();
        if (TextUtils.isEmpty(register_code)) {
            ToastUtils.showShort("验证码为空,请重新填写验证码");
            return;
        }

        BmobSMS.verifySmsCode(this, phone, register_code, new VerifySMSCodeListener() {


            @Override
            public void done(cn.bmob.sms.exception.BmobException ex) {
                if (ex == null) {//短信验证码已验证成功
                    Log.e("bmob", "验证通过");
                    ToastUtil.showShortToast(mContext, "短信验证已通过");
                    //重置密码!
                    resetPwd();
                } else {
                    ToastUtil.showShortToast(mContext, "短信验证失败");
                    Log.e("bmob", "验证失败:code =" + ex.getErrorCode() + ",msg = " + ex.getLocalizedMessage());
                }
            }


        });
    }

    private void resetPwd() {
        final String et_phone = username.getText().toString().trim();
        if (TextUtils.isEmpty(et_phone)) {
            ToastUtils.showShort(R.string.plz_input_phone);
            return;
        }
        String pwd = et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.showShort(R.string.plz_input_pwd);
            return;
        }
        String pwdAgainStr = pwdAgain.getText().toString().trim();
        if (TextUtils.isEmpty(pwdAgainStr)) {
            ToastUtils.showShort(R.string.plz_input_pwd_again);
            return;
        }
        if (!pwd.equals(pwdAgainStr)) {
            ToastUtils.showShort(R.string.pwd_input_diff);
            return;
        }
        DialogUtil.progressDialog(mContext, getString(R.string.reset_password_now), false);
        BmobUser newUser = new BmobUser();
        newUser.setPassword(pwd);
        BmobUser bmobUser = BmobUser.getCurrentUser();
        newUser.update(bmobUser.getObjectId(),new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtils.showShort(R.string.reset_success_password);
                    userRxPreferences.getString(Constants.LOGIN_PHONE).set(et_phone);
                    startActivity(new Intent(mContext,LoginActivity.class));
                    finish();
                } else {
                    Log.e(TAG, "done: "+e.getErrorCode() + ":" + e.getMessage() );
                    if (203 == e.getErrorCode()) {
                        ToastUtils.showShort(R.string.phone_already_register);
                    } else {
                        ToastUtils.showShort(R.string.reset_failure);
                    }
                }
                DialogUtil.hide();
            }

        });

    }

    public  interface UpdateUIVericationCode {
    public void  setupdateUIVericationCode();
}
    private UpdateUIVericationCode myListener;
    //回调方法
    public void setOnListener(UpdateUIVericationCode myListener) {
        this.myListener = myListener;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值