智能管家---11. 用户重置密码功能实现(忘记密码或重置)

接下来实现重置密码,图解:
这里写图片描述
所以分两步走,有原始密码或没有原始密码。

  • 重置密码界面
    这里写图片描述
    xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改密码"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"/>

    <EditText
        android:id="@+id/et_now"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="旧密码"/>

    <EditText
        android:id="@+id/et_new"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="新密码"/>

    <EditText
        android:id="@+id/et_new_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="再次输入密码"/>

    <Button
        android:id="@+id/btn_update_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_bg"
        android:text="重置密码"
        android:textColor="@android:color/white"/>

    <TextView
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="忘记密码"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp"/>

    <EditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="邮箱"/>

    <Button
        android:id="@+id/btn_forget_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_bg"
        android:text="确认"
        android:textColor="@android:color/white"/>


</LinearLayout>
  • 具体实现,重置密码也是获取用户输入的内容,或直接修改密码,或通过邮箱重置
  • 直接修改密码
    这里写图片描述
  • 邮箱重置
    这里写图片描述

  • ForgetPasswordActivity源代码

public class ForgetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_now,et_new,et_new2;
    private EditText et_email;
    private Button btn_update;
    private Button btn_forget;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);

        initView();
    }

    private void initView() {
        et_now=(EditText)findViewById(R.id.et_now);
        et_new=(EditText)findViewById(R.id.et_new);
        et_new2=(EditText)findViewById(R.id.et_new_password);
        et_email=(EditText)findViewById(R.id.et_email);
        btn_update=(Button)findViewById(R.id.btn_update_password);
        btn_forget=(Button)findViewById(R.id.btn_forget_password);

        btn_update.setOnClickListener(this);
        btn_forget.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_update_password:
                //重置密码
                String now=et_now.getText().toString().trim();
                String pass1=et_new.getText().toString().trim();
                String pass2=et_new2.getText().toString().trim();

                if(!TextUtils.isEmpty(now)&&!TextUtils.isEmpty(pass1)&&!TextUtils.isEmpty(pass2)){

                    //两次密码一致
                    if(pass1.equals(pass2)){
                        MyUSer.updateCurrentUserPassword(now, pass1, new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if(e==null){
                                    Toast.makeText(ForgetPasswordActivity.this, "密码重置成功!", Toast.LENGTH_SHORT).show();
                                    finish();
                                }else{
                                    Toast.makeText(ForgetPasswordActivity.this, "密码重置失败!", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }else{
                        Toast.makeText(ForgetPasswordActivity.this, "两次密码输入不一致!", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(ForgetPasswordActivity.this, "输入框不能为空!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_forget_password:
                String email=et_email.getText().toString().trim();
                if(!TextUtils.isEmpty(email)){
                    //发送邮件重置密码
                    MyUSer.resetPasswordByEmail(email, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                                Toast.makeText(ForgetPasswordActivity.this, "请前往邮箱查看!", Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(ForgetPasswordActivity.this, "密码重置失败!", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else{
                    Toast.makeText(ForgetPasswordActivity.this, "输入框不能为空!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ayangann915/article/details/81486600
今日推荐