1. 程式人生 > >Android 基於bmob平臺的手機登入驗證碼註冊 等功能實現

Android 基於bmob平臺的手機登入驗證碼註冊 等功能實現

 

 

首先是基於Bmob 平臺,採用MVP 開發模式。效果見圖:

 

 

 

 

 

下面一 一給出原始碼:

 

MVP 目錄結構:

 

登入/註冊的Activity:

package com.example.yangzi.mvp_dev.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.example.yangzi.mvp_dev.R;
import com.example.yangzi.mvp_dev.base.BaseActivity;
import com.example.yangzi.mvp_dev.bean.remote.MyUser;
import com.example.yangzi.mvp_dev.mvp.presenter.Imp.UserLogPresenterImp;
import com.example.yangzi.mvp_dev.mvp.presenter.UserLogPresenter;
import com.example.yangzi.mvp_dev.mvp.view.UserLogView;
import com.example.yangzi.mvp_dev.utils.LogUtils;
import com.example.yangzi.mvp_dev.utils.ProgressUtils;
import com.example.yangzi.mvp_dev.utils.RegexUtils;
import com.example.yangzi.mvp_dev.utils.SnackbarUtils;
import com.example.yangzi.mvp_dev.utils.StringUtils;
import com.example.yangzi.mvp_dev.utils.ToastUtils;
import com.example.yangzi.mvp_dev.widget.OwlView;
import com.example.yangzi.mvp_dev.widget.VerifyCodeButton;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

/**
 * Created by linzihahaha on 2018/12/8.
 */
public class LoginActivity extends BaseActivity implements UserLogView {

    @BindView(R.id.owl_view)
    OwlView mOwlView;
    @BindView(R.id.login_et_username)
    TextInputLayout usernameET;
    @BindView(R.id.login_et_password)
    EditText passwordET;
    @BindView(R.id.login_tv_forget)
    TextView forget;
    @BindView(R.id.login_et_rpassword)
    EditText rpasswordET;
    @BindView(R.id.login_et_phone)
    TextInputLayout phone;
    @BindView(R.id.login_et_email)
    TextInputLayout email;
    @BindView(R.id.login_get_code)
    TextInputLayout getCode;
    @BindView(R.id.login_tv_sign)
    TextView signTV;
    @BindView(R.id.login_tv_phone)
    TextView loginphoneTV;

    @BindView(R.id.btn_login_get_code)
    VerifyCodeButton getcodeBtn;
    @BindView(R.id.login_btn_login)
    Button loginBtn;
    private EditText mEdtName;
    private EditText mEdtPhone;
    private EditText mEdtEmail;
    private EditText mEdtCode;
    private EditText mEdtPassword;
    private EditText mEdtrPassword;
    //是否是登陸操作
    private boolean isLogin = true;
    private boolean isLoginbyphone = false;

    private UserLogPresenter userLogPresenter;

    @Override
    protected int getLayout() {
        return R.layout.activity_user_login;
    }


    @Override
    protected void initEventAndData() {

        userLogPresenter = new UserLogPresenterImp(this);
        mEdtName = usernameET.getEditText();
        mEdtPhone = phone.getEditText();
        mEdtEmail=email.getEditText();
        mEdtCode = getCode.getEditText();
        mEdtName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                usernameET.setErrorEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mEdtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!RegexUtils.checkPhone(phone)) {
                    mEdtPhone.setError("請輸入正確的手機號");
                } else {
                    getCode.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mEdtEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!RegexUtils.checkPhone(phone)) {
                    mEdtPhone.setError("請輸入正確的郵箱");
                } else {
                    getCode.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mEdtCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String code = charSequence.toString();
                if (!RegexUtils.checkCode(code)) {
                    getCode.setError("請輸入正確的驗證碼");
                } else {
                    getCode.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    //監聽密碼輸入框的聚焦事件
    @OnFocusChange({R.id.login_et_password,R.id.login_et_rpassword})
    public void onFocusChange(View view, boolean b) {
        if (b) {
            mOwlView.open();
        } else {
            mOwlView.close();
        }
    }

    /**
     * 監聽點選事件
     *
     * @param view
     */
    @OnClick({R.id.btn_login_get_code,R.id.login_tv_sign, R.id.login_btn_login, R.id.login_tv_forget,R.id.login_tv_phone})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login_get_code: // 獲取驗證碼

                getCode();
                break;
            case R.id.login_btn_login:  //button
                if (isLogin) {

                    login();  //登陸
                } else {
                    sign();  //註冊
                }
                break;
            case R.id.login_tv_forget:  //忘記密碼
                startActivity(new Intent(mContext, ForgetPasswordActivity.class));
                break;
            case R.id.login_tv_sign:  //sign
                if (!isLogin) {

                    //置換註冊介面
                    if (!isLoginbyphone) {
                        loginBtn.setText("登入");
                        signTV.setText("郵箱註冊");

                        loginphoneTV.setText("驗證碼註冊");
                        usernameET.setVisibility(View.VISIBLE);
                        passwordET.setVisibility(View.VISIBLE);
                        rpasswordET.setVisibility(View.GONE);
                        phone.setVisibility(View.GONE);
                        email.setVisibility(View.GONE);
                        getCode.setVisibility(View.GONE);
                        forget.setVisibility(View.VISIBLE);
                        getcodeBtn.setVisibility(View.GONE);
                        usernameET.setHint("使用者名稱/手機號/郵箱");
                    }else{
                        loginBtn.setText("驗證");
                        signTV.setText("郵箱註冊");
                        loginphoneTV.setText("密碼登入");
                        usernameET.setVisibility(View.GONE);
                        passwordET.setVisibility(View.GONE);
                        rpasswordET.setVisibility(View.GONE);
                        phone.setVisibility(View.VISIBLE);
                        email.setVisibility(View.GONE);
                        getCode.setVisibility(View.VISIBLE);
                        forget.setVisibility(View.GONE);
                        getcodeBtn.setVisibility(View.VISIBLE);
                        usernameET.setHint("使用者名稱");
                    }
                } else {
                    //置換登陸介面
                    loginBtn.setText("註冊");
                    signTV.setText("返回登入");

                    usernameET.setVisibility(View.VISIBLE);
                    passwordET.setVisibility(View.VISIBLE);
                    rpasswordET.setVisibility(View.VISIBLE);
                    phone.setVisibility(View.GONE);
                    email.setVisibility(View.VISIBLE);
                    getCode.setVisibility(View.GONE);
                    forget.setVisibility(View.GONE);
                    getcodeBtn.setVisibility(View.GONE);
                    usernameET.setHint("使用者名稱");
                }
                isLogin = !isLogin;
                break;
            case R.id.login_tv_phone:  //手機號驗證碼登入
                isLogin=true;
                if (isLoginbyphone) {
                    loginBtn.setText("登入");
                    signTV.setText("郵箱註冊");
                    loginphoneTV.setText("驗證碼註冊");
                    usernameET.setVisibility(View.VISIBLE);
                   passwordET.setVisibility(View.VISIBLE);
                    rpasswordET.setVisibility(View.GONE);
                    phone.setVisibility(View.GONE);
                    email.setVisibility(View.GONE);
                    getCode.setVisibility(View.GONE);
                    forget.setVisibility(View.VISIBLE);
                    getcodeBtn.setVisibility(View.GONE);
                    usernameET.setHint("使用者名稱/手機號/郵箱");
                }else{
                    loginBtn.setText("驗證");
                    signTV.setText("郵箱註冊");
                    loginphoneTV.setText("密碼登入");
                    usernameET.setVisibility(View.GONE);
                   passwordET.setVisibility(View.GONE);
                    rpasswordET.setVisibility(View.GONE);
                    phone.setVisibility(View.VISIBLE);
                    email.setVisibility(View.GONE);
                    getCode.setVisibility(View.VISIBLE);
                    forget.setVisibility(View.GONE);
                    getcodeBtn.setVisibility(View.VISIBLE);
                    usernameET.setHint("使用者名稱");
                }
                isLoginbyphone=!isLoginbyphone;
                break;
            default:
                break;
        }
    }

    private void getCode() {
        String username = mEdtName.getText().toString();
        String password = passwordET.getText().toString();
        String rpassword = rpasswordET.getText().toString();
        final String phonenumber = mEdtPhone.getText().toString();
        if (phonenumber.length() == 0) {
            Toast.makeText(mContext, "請輸入手機號", Toast.LENGTH_SHORT).show();
            return;
        }
        BmobQuery<MyUser> query = new BmobQuery<MyUser>();
        query.addWhereEqualTo("mobilePhoneNumber", phonenumber);
        query.setLimit(1);
        query.findObjects(new FindListener<MyUser>() {
            @Override
            public void done(List<MyUser> list, BmobException e) {
                if (e == null) {
                    if (list.size()>0)
                         Toast.makeText(mContext, "手機號已註冊,請返回登入", Toast.LENGTH_SHORT).show();
                    else
                        requestSMScodeFromBmob(phonenumber);
                }
            }
        });

// 請求驗證碼,其中country表示國家程式碼,如“86”;phone表示手機號碼,如“13800138000”
        //Toast.makeText(mContext, "傳送驗證碼請求" + username, Toast.LENGTH_SHORT).show();
        //SMSSDK.getVerificationCode("86",username);

    }
    /**
     * 請求驗證碼
     * @param phone_number 手機號
     * @param template_name 驗證碼模板名稱
     * @param listener 回撥
     */
    /**
     * TODO template 如果是自定義簡訊模板,此處替換為你在控制檯設定的自定義簡訊模板名稱;如果沒有對應的自定義簡訊模板,則使用預設簡訊模板。
     */
    public void requestSMScodeFromBmob(String phone) {
        getcodeBtn.start();
        BmobSMS.requestSMSCode(phone, "register", new QueryListener<Integer>() {
            @Override
            public void done(Integer smsId, BmobException e) {
                if (e == null) {
                    Toast.makeText(mContext, "傳送驗證碼成功,簡訊ID:" + smsId + "\n", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "傳送驗證碼失敗:" + e.getErrorCode() + "-" + e.getMessage() + "\n", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 執行登陸動作
     */
    public void login() {

        if(!isLoginbyphone) {
            String username = mEdtName.getText().toString();
            String password = passwordET.getText().toString();

            if (username.length() == 0 || password.length() == 0 ) {
                SnackbarUtils.show(mContext, "輸入為空或格式不正確");
                return;
            }

            ProgressUtils.show(this, "正在登陸...");
            userLogPresenter.login(username, password);
        }else{
            String phonenumber = mEdtPhone.getText().toString();
            String code = mEdtCode.getText().toString();
            if (phonenumber.length() == 0 || !RegexUtils.checkPhone(phonenumber)) {
                Toast.makeText(mContext, "請輸入正確手機號", Toast.LENGTH_SHORT).show();
                return;
            }
            if (code.length() == 0|| !RegexUtils.checkCode(code)) {
                Toast.makeText(mContext, "請輸入正確驗證碼", Toast.LENGTH_SHORT).show();
                return;
            }
            ProgressUtils.show(this, "正在驗證...");
            veriflySMSCode(phonenumber,code);

        }
    }

    /**
     * 執行註冊動作
     */
    public void sign() {
        String username = mEdtName.getText().toString();
        String password = passwordET.getText().toString();
        String rpassword = rpasswordET.getText().toString();
        String email= mEdtEmail.getText().toString();
        String phone = mEdtPhone.getText().toString();

        if ( username.length() == 0 || password.length() == 0 || rpassword.length() == 0) {
            SnackbarUtils.show(mContext, "請填寫必要資訊");
            return;
        }
        if (!RegexUtils.checkUserName(username)) {
            SnackbarUtils.show(mContext, "請輸入正確使用者名稱 6到18位字母開頭與字數組合");
            return;
        }
        //ToastUtils.show(mContext,"xxxx"+password+"||"+rpassword);
        if (!password.equals(rpassword)|| !RegexUtils.checkPassword(password)) {
            SnackbarUtils.show(mContext, "兩次密碼不一致");
            return;
        }
        ProgressUtils.show(this, "正在註冊...");
        if (!isLogin && !isLoginbyphone)  {
            emailVerify(email);
            userLogPresenter.signup(username,password,email);
        }else
        userLogPresenter.signup(username,password,phone);

    }
    /**
     * 傳送驗證郵件
     */
    private void emailVerify(final String email) {

        MyUser.requestEmailVerify(email, new UpdateListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtils.show(mContext, "請求驗證郵件成功,請到" + email + "郵箱中進行啟用賬戶。");
                } else {
                    Log.e("BMOB", e.toString());
                    ToastUtils.show(mContext, "請求驗證郵件失敗," + e.getMessage());
                }
            }
        });
    }

    private void veriflySMSCode(final String phonenumber,final String code) {
        BmobSMS.verifySmsCode(phonenumber, code, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtils.show(mContext, "驗證碼驗證成功");
                    ProgressUtils.dismiss();
                    if(phone.isShown())
                    {
                        //ToastUtils.show(mContext, "該手機號第一次登入,請設定使用者名稱密碼");
                        isLogin=false;
                        loginBtn.setText("註冊");
                        loginphoneTV.setText("密碼登入");
                        usernameET.setVisibility(View.VISIBLE);
                        passwordET.setVisibility(View.VISIBLE);
                        rpasswordET.setVisibility(View.VISIBLE);
                        phone.setVisibility(View.GONE);
                        getCode.setVisibility(View.GONE);
                        forget.setVisibility(View.VISIBLE);
                        getcodeBtn.setVisibility(View.GONE);
                        usernameET.setHint("使用者名稱");
                    }

                } else {
                    ProgressUtils.dismiss();
                    ToastUtils.show(mContext, "驗證碼有誤"+e.getErrorCode() + "-" + e.getMessage());
                }
            }
        });
    }


    @Override
    public void loadDataSuccess(MyUser tData) {
        ProgressUtils.dismiss();
        if (isLogin) {
                ToastUtils.show(mContext, "登入成功");
                setResult(RESULT_OK, new Intent());
                finish();

        }else {
            ToastUtils.show(mContext, "註冊成功");
            if (tData.getEmail()==null){
                setResult(RESULT_OK, new Intent());
                finish();
            }else {
                //emailVerify(tData.getEmail());
                SnackbarUtils.show(mContext, "請先登陸郵箱驗證後登陸");
            }
        }

    }

    @Override
    public void loadDataError(Throwable throwable) {
        ProgressUtils.dismiss();
        SnackbarUtils.show(mContext, throwable.getMessage());
        Log.v(LogUtils.filename(new Exception()),LogUtils.funAndLine(new Exception())+throwable.getMessage());
    }

}

使用者資訊展示Activity:

package com.example.yangzi.mvp_dev.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.yangzi.mvp_dev.R;
import com.example.yangzi.mvp_dev.base.BaseActivity;
import com.example.yangzi.mvp_dev.bean.remote.MyUser;
import com.example.yangzi.mvp_dev.common.Constants;
import com.example.yangzi.mvp_dev.mvp.presenter.Imp.UserInfoPresenterImp;
import com.example.yangzi.mvp_dev.mvp.presenter.UserInfoPresenter;
import com.example.yangzi.mvp_dev.mvp.view.UserInfoView;
import com.example.yangzi.mvp_dev.utils.ImageUtils;
import com.example.yangzi.mvp_dev.utils.LogUtils;
import com.example.yangzi.mvp_dev.utils.ProgressUtils;
import com.example.yangzi.mvp_dev.utils.SnackbarUtils;
import com.example.yangzi.mvp_dev.utils.StringUtils;
import com.example.yangzi.mvp_dev.widget.CommonItemLayout;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import cn.smssdk.gui.util.Const;

/**
 * Created by linzihahaha on 2018/12/24.
 * 使用者資訊管理中心
 */
public class UserInfoActivity extends BaseActivity implements UserInfoView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rlt_update_icon)
    RelativeLayout iconRL;
    @BindView(R.id.img_icon)
    ImageView iconIv;
    @BindView(R.id.cil_username)
    CommonItemLayout usernameCL;
    @BindView(R.id.cil_sex)
    CommonItemLayout sexCL;
    @BindView(R.id.cil_phone)
    CommonItemLayout phoneCL;
    @BindView(R.id.cil_email)
    CommonItemLayout emailCL;

    private UserInfoPresenter presenter;

    //選擇圖片來源
    private AlertDialog iconDialog;
    private AlertDialog genderDialog;
    private AlertDialog phoneDialog;
    private AlertDialog emailDialog;

    protected static final int CHOOSE_PICTURE = 0;
    protected static final int TAKE_PICTURE = 1;
    protected static final int GENDER_MAN = 0;
    protected static final int GENDER_FEMALE = 1;
    private static final int CROP_SMALL_PICTURE = 2;

    //圖片路徑
    protected static Uri tempUri = null;
    @Override
    protected int getLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initEventAndData() {
        presenter = new UserInfoPresenterImp(this);
        presenter.showUserInfor(toolbar,currentUser);





    }

    @Override
    public void loadDataSuccess(MyUser tData) {
        ProgressUtils.dismiss();
    }

    @Override
    public void loadDataError(Throwable throwable) {
        ProgressUtils.dismiss();
        SnackbarUtils.show(mContext, throwable.getMessage());
    }

    @Override
    public void showUserInfor(Toolbar toolbar, MyUser user) {
        //初始化Toolbar
        toolbar.setTitle("賬戶");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回訊息更新上個Activity資料
                setResult(RESULT_OK, new Intent());
                finish();
            }
        });
        if (currentUser != null) {
            //載入到佈局中
            //載入當前頭像
            Glide.with(mContext).load(currentUser.getImage()).into(iconIv);
            usernameCL.setRightText(currentUser.getUsername());
            sexCL.setRightText(currentUser.getGender());
            phoneCL.setRightText(currentUser.getMobilePhoneNumber());
            emailCL.setRightText(currentUser.getEmail());

        }

    }
    /**
     * 監聽點選事件
     *
     * @param view
     */
    @OnClick({R.id.rlt_update_icon, R.id.cil_username,
            R.id.cil_sex, R.id.cil_phone, R.id.cil_email})
    public void onViewClicked(final View view) {
        switch (view.getId()) {
            case R.id.rlt_update_icon:  //頭像
//                showIconDialog();
                Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
                openAlbumIntent.setType("image/*");
                startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
                break;
            case R.id.cil_username:  //使用者名稱
                SnackbarUtils.show(mContext, "江湖人行不更名,坐不改姓!");
                break;
            case R.id.cil_sex:  //性別
                showGenderDialog();
                break;
            case R.id.cil_phone:  //電話修改

                showPhoneDialog();
                break;
            case R.id.cil_email:  //郵箱修改
                showMailDialog();
                break;
            default:
                break;
        }
    }
    /**
     * 更新使用者資料
     */
    public void doUpdate() {
        if (currentUser == null)
            return;
        Log.v(LogUtils.filename(new Exception()),LogUtils.funAndLine(new Exception())+"doUpdate");
        ProgressUtils.show(UserInfoActivity.this, "正在修改...");

        presenter.update(currentUser);

    }
    /**
     * 顯示選擇性別對話方塊
     */
    public void showGenderDialog() {
        if (genderDialog == null) {
            genderDialog = new AlertDialog.Builder(this).setItems(new String[]{"男", "女"},
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case GENDER_MAN: // 男性
                                        currentUser.setGender("男");
                                        doUpdate();
                                    break;
                                case GENDER_FEMALE: // 女性
                                        currentUser.setGender("女");
                                        doUpdate();
                                    break;
                            }
                            sexCL.setRightText(currentUser.getGender());
                        }
                    }).create();
        }
        if (!genderDialog.isShowing()) {
            genderDialog.show();
        }
    }

    /**
     * 顯示更換電話對話方塊
     */
    public void showPhoneDialog() {
        final EditText editText = new EditText(mContext);
        String phone = currentUser.getMobilePhoneNumber();
        if (phone != null) {
            SnackbarUtils.show(mContext, "已繫結,可進入設定更換!");
            return;
        }
        if (phoneDialog == null) {
            phoneDialog = new AlertDialog.Builder(this)
                    .setTitle("繫結手機號")
                    .setMessage("請輸入新手機號")
                    .setView(editText)
                    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            final String input = editText.getText().toString();
                            if (input.equals("")) {
                                Toast.makeText(getApplicationContext(), "內容不能為空!" + input,
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                if (StringUtils.checkPhoneNumber(input)) {
                                    BmobQuery<MyUser> query = new BmobQuery<MyUser>();
                                    query.addWhereEqualTo("mobilePhoneNumber", input);
                                    query.setLimit(1);
                                    query.findObjects(new FindListener<MyUser>() {
                                        @Override
                                        public void done(List<MyUser> list, BmobException e) {
                                            if (e == null) {
                                                if (list.size()>0)
                                                    Toast.makeText(mContext, "手機號已註冊!", Toast.LENGTH_SHORT).show();
                                                else{
                                                    verifyPhone(Constants.VERI_ADD_PHONE,input);
                                                }

                                            }
                                        }
                                    });
                                } else {
                                    Toast.makeText(UserInfoActivity.this,
                                            "請輸入正確的電話號碼", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    })
                    .setNegativeButton("取消", null)
                    .create();
        }
        if (!phoneDialog.isShowing()) {
            phoneDialog.show();
        }
    }
    /*
    *手機號碼驗證
     */
    private void verifyPhone(int index,String input) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("phoneNumber",input);
        bundle.putString("index",""+index);
        intent.putExtras(bundle);
        intent.setClass(this, VerifyPhoneActivity.class);
        startActivityForResult(intent,index);
    }

    /**
     * 顯示更換郵箱對話方塊
     */
    public void showMailDialog() {
        final EditText emailEditText = new EditText(UserInfoActivity.this);
        emailEditText.setText(currentUser.getEmail());
        String title = "新增郵箱";
        if(currentUser.getEmail()!=null) {
             title = "更換郵箱";
            //將游標移至文字末尾
            emailEditText.setSelection(currentUser.getEmail().length());
        }
        if (emailDialog == null) {
            emailDialog = new AlertDialog.Builder(this)
                    .setTitle(title)
                    .setView(emailEditText)
                    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            final String input = emailEditText.getText().toString();
                            if (input.equals("")) {
                                Toast.makeText(getApplicationContext(), "內容不能為空!" + input,
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                if (StringUtils.checkEmail(input)) {
                                    currentUser.setEmail(input);
                                    BmobQuery<MyUser> query = new BmobQuery<MyUser>();
                                    query.addWhereEqualTo("mobilePhoneNumber", input);
                                    query.setLimit(1);
                                    query.findObjects(new FindListener<MyUser>() {
                                        @Override
                                        public void done(List<MyUser> list, BmobException e) {
                                            if (e == null) {
                                                if (list.size()>0)
                                                    Toast.makeText(mContext, "郵箱已註冊!", Toast.LENGTH_SHORT).show();
                                                else{
                                                    //Toast.makeText(mContext, "請登入郵箱啟用!", Toast.LENGTH_SHORT).show();

                                                    emailCL.setRightText(input);
                                                    doUpdate();
                                                }

                                            }
                                        }
                                    });

                                } else {
                                    Toast.makeText(UserInfoActivity.this,
                                            "請輸入正確的郵箱格式", Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    })
                    .setNegativeButton("取消", null)
                    .create();
        }
        if (!emailDialog.isShowing()) {
            emailDialog.show();
        }
    }

    /**
     * 監聽Activity返回結果
     *
     * @param requestCode
     * @param resultCode
     * @param intent
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case TAKE_PICTURE:
                    startPhotoZoom(tempUri); // 開始對圖片進行裁剪處理
                    break;
                case CHOOSE_PICTURE:
                    startPhotoZoom(intent.getData()); // 開始對圖片進行裁剪處理
                    break;
                case CROP_SMALL_PICTURE:
                    if (intent != null) {
                        setImageToView(intent); // 讓剛才選擇裁剪得到的圖片顯示在介面上
                    }
                    break;
                case Constants.VERI_ADD_PHONE :
                    Log.v(LogUtils.filename(new Exception()),LogUtils.funAndLine(new Exception())+intent.getStringExtra("phone"));
                    //Toast.makeText(mContext, "手機繫結成功", Toast.LENGTH_SHORT).show();
                        currentUser.setMobilePhoneNumber(intent.getStringExtra("phone"));
                         phoneCL.setRightText(intent.getStringExtra("phone"));
                        doUpdate();

                    break;
            }
        }
    }

    /**
     * 監聽Back鍵按下事件,方法2:
     * 注意:
     * 返回值表示:是否能完全處理該事件
     * 在此處返回false,所以會繼續傳播該事件.
     * 在具體專案中此處的返回值視情況而定.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
            setResult(RESULT_OK, new Intent());
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 拍照
     */
    private void takePicture() {
        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (Build.VERSION.SDK_INT >= 23) {
            // 需要申請動態許可權
            int check = ContextCompat.checkSelfPermission(this, permissions[0]);
            // 許可權是否已經 授權 GRANTED---授權  DINIED---拒絕
            if (check != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            }
        }
        Intent openCameraIntent = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(), "image.jpg");
        //判斷是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= 24) {
            openCameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            tempUri = FileProvider.getUriForFile(UserInfoActivity.this,
                    "com.copasso.cocobill.fileProvider", file);
        } else {
            tempUri = Uri.fromFile(new File(Environment
                    .getExternalStorageDirectory(), "image.jpg"));
        }
        // 指定照片儲存路徑(SD卡),image.jpg為一個臨時檔案,每次拍照後這個圖片都會被替換
        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
        startActivityForResult(openCameraIntent, TAKE_PICTURE);
    }

    /**
     * 裁剪圖片方法實現
     *
     * @param uri
     */
    protected void startPhotoZoom(Uri uri) {
        if (uri == null) {
            Log.i("tag", "The uri is not exist.");
        }
        tempUri = uri;
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 設定裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是寬高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪圖片寬高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CROP_SMALL_PICTURE);
    }

    /**
     * 儲存裁剪之後的圖片資料
     *
     * @param data
     */
    protected void setImageToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            photo = ImageUtils.toRoundBitmap(photo); // 這個時候的圖片已經被處理成圓形的了
            iconIv.setImageBitmap(photo);
            uploadPic(photo);
        }
    }

    /**
     * 儲存頭像並上傳伺服器
     *
     * @param bitmap
     */
    private void uploadPic(Bitmap bitmap) {
        // 上傳至伺服器
        // 可以在這裡把Bitmap轉換成file,然後得到file的url,做檔案上傳操作
        // 注意這裡得到的圖片已經是圓形圖片了
        // bitmap是沒有做個圓形處理的,但已經被裁剪了
        String imagename = currentUser.getObjectId() + "_" + String.valueOf(System.currentTimeMillis());
        String imagePath = ImageUtils.savePhoto(bitmap, Environment
                .getExternalStorageDirectory().getAbsolutePath(), imagename + ".png");
        if (imagePath != null) {
            final BmobFile bmobFile = new BmobFile(new File(imagePath));
            bmobFile.uploadblock(new UploadFileListener() {
                @Override
                public void done(BmobException e) {
                    if (e==null) {
                        MyUser newUser=new MyUser();
                        newUser.setImage(bmobFile.getFileUrl());
                        newUser.update(currentUser.getObjectId(),new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e!=null)
                                    Log.i(TAG,e.getMessage());
                            }
                        });
                    }else{
                        Log.i(TAG,e.getMessage());
                    }
                }
            });
        }
    }

    /**
     * 許可權請求
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        } else {
            // 沒有獲取 到許可權,從新請求,或者關閉app
            Toast.makeText(this, "需要儲存許可權", Toast.LENGTH_SHORT).show();
        }
    }
}

setting 的activity:

package com.example.yangzi.mvp_dev.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.example.yangzi.mvp_dev.R;
import com.example.yangzi.mvp_dev.base.BaseActivity;
import com.example.yangzi.mvp_dev.bean.remote.MyUser;
import com.example.yangzi.mvp_dev.common.Constants;
import com.example.yangzi.mvp_dev.utils.GlideCacheUtil;
import com.example.yangzi.mvp_dev.utils.LogUtils;
import com.example.yangzi.mvp_dev.utils.ProgressUtils;
import com.example.yangzi.mvp_dev.utils.SnackbarUtils;
import com.example.yangzi.mvp_dev.utils.ToastUtils;
import com.example.yangzi.mvp_dev.widget.CommonItemLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by linzihahaha on 2018/12/30.
 */
public class SettingActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.cil_change)
    CommonItemLayout changeCL;
    @BindView(R.id.cil_forget)
    CommonItemLayout forgetCL;
    @BindView(R.id.cil_store)
    CommonItemLayout storeCL;
    @BindView(R.id.cil_sort)
    CommonItemLayout sortCL;
    @BindView(R.id.cil_pay)
    CommonItemLayout payCL;
    @BindView(R.id.cil_export)
    CommonItemLayout exportCL;

    private AlertDialog pwDialog;
    private AlertDialog cacheDialog;

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initEventAndData() {

        //初始化Toolbar
        toolbar.setTitle("設定");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回訊息更新上個Activity資料
                //setResult(RESULT_OK, new Intent());
                finish();
            }
        });

        storeCL.setRightText(GlideCacheUtil.getInstance().getCacheSize(mContext));
    }

    /**
     * 監聽點選事件
     * @param view*/

    @OnClick({R.id.cil_changePhone,R.id.cil_forget,R.id.cil_change,R.id.cil_store,
            R.id.cil_sort,R.id.cil_pay,R.id.cil_export,R.id.cil_aboutus})
    public void onViewClicked(final View view) {
        switch (view.getId()) {
            case R.id.cil_changePhone:
                verifyPhone(Constants.VERI_CHANGE_PHONE);
                break;
            case R.id.cil_change:  //修改密碼
                verifyPhone(Constants.VERI_CHANGE_PWD);
                break;
            case R.id.cil_forget:  //忘記密碼
//                startActivity(new Intent(this,ForgetPasswordActivity.class));
                showForgetPwDialog();
                break;
            case R.id.cil_store:  //快取
                showCacheDialog();
                break;
            case R.id.cil_aboutus:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            default:
                break;
        }
    }

    private void verifyPhone(int index) {
        if (!MyUser.isLogin()) {
            Toast.makeText(getApplicationContext(), "請先登入!",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        String phonenunber=currentUser.getMobilePhoneNumber();
        if (phonenunber==null)
        {
            Toast.makeText(getApplicationContext(), "請先繫結手機號!" ,
                    Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("phoneNumber",phonenunber);
        intent.putExtra("index",""+index);
        intent.setClass(this, VerifyPhoneActivity.class);
        startActivityForResult(intent,index);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.VERI_CHANGE_PWD && resultCode == Activity.RESULT_OK) {
            showChangePwdDialog();
        }else if (requestCode == Constants.VERI_CHANGE_PHONE && resultCode == Activity.RESULT_OK) {
            Log.v(LogUtils.filename(new Exception()),LogUtils.funAndLine(new Exception())+data.getStringExtra("phone"));
            changePhone(data.getStringExtra("phone"));
        }

    }
    /**
     * 顯示忘記密碼對話方塊
     */
    public void showForgetPwDialog() {
        final EditText editText = new EditText(mContext);
        editText.setHint("請輸入註冊郵箱");
        //彈出輸入框
        new AlertDialog.Builder(this)
                .setTitle("忘記密碼")
                .setView(editText)
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String input = editText.getText().toString();
                        if (input.equals("")) {
                            SnackbarUtils.show(mContext, "內容不能為空!");
                        }else{
                            //找回密碼
                            BmobUser.resetPasswordByEmail(input, new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    if(e==null){
                                        ToastUtils.show(mContext,"重置密碼請求成功,請到郵箱進行密碼重置操作");
                                    }else{
                                        ToastUtils.show(mContext,"失敗:" + e.getMessage());
                                    }
                                }
                            });
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    /**
     * 顯示修改密碼對話方塊
     */
    public void showChangePwdDialog() {
        final LinearLayout layout=new LinearLayout(mContext);
        //載入佈局
        View view= LayoutInflater.from(mContext).inflate(R.layout.dialog_change_password,layout,false);
        TextInputLayout til=(TextInputLayout)view.findViewById(R.id.change_til_password);
        TextInputLayout til1=(TextInputLayout)view.findViewById(R.id.change_til_repassword);

        final EditText editText = til.getEditText();
        final EditText editText1 = til1.getEditText();
        //加入檢視
        layout.addView(view);

        if (pwDialog == null) {
            pwDialog = new AlertDialog.Builder(this)
                    .setTitle("修改密碼")
                    .setView(layout)
                    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String input = editText.getText().toString();
                            String input1 = editText1.getText().toString();
                            if (input.equals("")||input1.equals("")) {
                                Toast.makeText(getApplicationContext(), "不能為空!" + input,
                                        Toast.LENGTH_SHORT).show();
                            }else if(input.equals(input1)){
                                //修改密碼
                                changePw(input);
                            } else {
                                Toast.makeText(mContext,
                                        "兩次輸入不一致", Toast.LENGTH_LONG).show();
                            }
                        }
                    })
                    .setNegativeButton("取消", null)
                    .create();
        }
        if (!pwDialog.isShowing()) {
            pwDialog.show();
        }
    }

    /**
     * 顯示清除快取對話方塊
     */
    public void showCacheDialog() {

        if (cacheDialog == null) {
            cacheDialog = new AlertDialog.Builder(this)
                    .setTitle("清除快取")
                    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            GlideCacheUtil.getInstance().clearImageDiskCache(mContext);
                        }
                    })
                    .setNegativeButton("取消", null)
                    .create();
        }
        if (!cacheDialog.isShowing()) {
            cacheDialog.show();
        }
    }

    /**
     * 更新使用者密碼
     */
    public void changePw(String password) {
        if (currentUser == null)
            return;

        ProgressUtils.show(mContext, "正在修改...");
        currentUser.setPassword(password);
        currentUser.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                ProgressUtils.dismiss();
                if (e!=null){
                    ToastUtils.show(mContext,"修改失敗"+e.getMessage());
                    Log.v(LogUtils.filename(new Exception()), LogUtils.funAndLine(new Exception()) +currentUser.getUsername()+ e.getMessage());
                } else{
                    ToastUtils.show(mContext,"修改成功");
                }
            }
        });
    }
    /**
     * 更新使用者手機號
     */
    public void changePhone(String phone) {
        if (currentUser == null)
            return;
        ProgressUtils.show(mContext, "正在修改...");
        currentUser.setMobilePhoneNumber(phone);
        currentUser.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                ProgressUtils.dismiss();
                if (e!=null){
                    ToastUtils.show(mContext,"修改失敗"+e.getMessage());
                    Log.v(LogUtils.filename(new Exception()), LogUtils.funAndLine(new Exception()) +currentUser.getUsername()+ e.getMessage());
                } else{
                    ToastUtils.show(mContext,"修改成功");
                }
            }
        });
    }
}

還有mainActivity:

 

package com.example.yangzi.mvp_dev.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.yangzi.mvp_dev.R;
import com.example.yangzi.mvp_dev.base.BaseActivity;
import com.example.yangzi.mvp_dev.bean.remote.MyUser;
import com.example.yangzi.mvp_dev.utils.LogUtils;
import com.example.yangzi.mvp_dev.utils.ThemeManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener{

    @BindView(R.id.toolbar)
    android.support.v7.widget.Toolbar toolbar;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;
    @BindView(R.id.nav_view)
    NavigationView navigationView;
    private View drawerHeader;
    private ImageView drawerIv;
    private TextView drawerTvAccount, drawerTvMail;
    protected static final int USERINFOACTIVITY_CODE = 0;
    protected static final int LOGINACTIVITY_CODE = 1;

    @Override
    protected int getLayout() {
         return R.layout.activity_main;
    }

    @Override
    protected void initEventAndData() {

        //初始化Toolbar
        toolbar.setTitle("MVP");
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        if (navigationView.getHeaderCount()>0) {
            drawerHeader =navigationView.getHeaderView(0);
        }else {
            drawerHeader = navigationView.inflateHeaderView(R.layout.drawer_header);
        }
        drawerIv = (ImageView) drawerHeader.findViewById(R.id.drawer_iv);
        drawerTvAccount = (TextView) drawerHeader.findViewById(R.id.drawer_tv_name);
        drawerTvMail = (TextView) drawerHeader.findViewById(R.id.drawer_tv_mail);

        //監聽點選登陸事件
        drawerHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                if (currentUser==null) {
                    //使用者id為0表示未有使用者登陸
                    startActivityForResult(new Intent(MainActivity.this, LoginActivity.class), LOGINACTIVITY_CODE);
                } else {
                    //進入賬戶中心
                    startActivityForResult(new Intent(MainActivity.this, UserInfoActivity.class), USERINFOACTIVITY_CODE);
                }
            }
        });
        //設定頭部賬戶
        setDrawerHeaderAccount();
        //監聽側滑選單
        navigationView.setNavigationItemSelectedListener(this);
    }
    /**
     * 設定DrawerHeader的使用者資訊
     */
    public void setDrawerHeaderAccount() {
        currentUser= BmobUser.getCurrentUser(MyUser.class);
        //獲取當前使用者
        if (currentUser != null) {
            drawerTvAccount.setText(currentUser.getUsername());
            drawerTvMail.setText(currentUser.getMobilePhoneNumber());
            if (currentUser.getImage()!=null) Glide.with(mContext).load(currentUser.getImage()).into(drawerIv);
        }else{
            drawerTvAccount.setText("賬號");
            drawerTvMail.setText("點我登陸");
            drawerIv.setImageResource(R.mipmap.ic_def_icon);
        }
    }
    /**
     * 監聽Drawer
     */
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 監聽點選事件 R.id.drawer_tv_name,R.id.drawer_tv_mail
     *
     * @param view
     */
    @OnClick({})
    public void onClick(View view) {
        switch (view.getId()) {

            default:
                break;
        }
    }

    /**
     * 監聽Activity返回值f
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case USERINFOACTIVITY_CODE:
                    setDrawerHeaderAccount();
                    break;
                case LOGINACTIVITY_CODE:
                    setDrawerHeaderAccount();
                    break;
            }
        }
    }
    /**
     * 顯示修改主題色 Dialog
     */
    private void showUpdateThemeDialog() {
        final String[] themes = ThemeManager.getInstance().getThemes();
        new AlertDialog.Builder(mContext)
                .setTitle("選擇主題")
                .setItems(themes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ThemeManager.getInstance().setTheme(mContext, themes[which]);

                    }
                }).create().show();
    }
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_exit) {      //退出登陸
            currentUser= BmobUser.getCurrentUser(MyUser.class);
            if (currentUser==null)
            {
                new AlertDialog.Builder(mContext).setTitle("當前賬戶未登入").show();
            }else {
                //退出登陸
                new AlertDialog.Builder(mContext).setTitle("是否退出當前賬戶")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                MyUser.logOut();
                                //清除本地資料
                                //LocalRepository.getInstance().deleteAllBills();
                                //重新整理賬戶資料
                                //setDrawerHeaderAccount();
                                initEventAndData();
                            }
                        })
                        .show();
            }
        } else if (id == R.id.nav_theme) {     //主題
            showUpdateThemeDialog();
        }else if (id == R.id.nav_setting) {   //設定
            startActivity(new Intent(this, SettingActivity.class));
        }
        return false;
    }
}

還有 驗證碼驗證的Activity:

package com.example.yangzi.mvp_dev.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yangzi.mvp_dev.R;
import com.example.yangzi.mvp_dev.base.BaseActivity;
import com.example.yangzi.mvp_dev.bean.remote.MyUser;
import com.example.yangzi.mvp_dev.mvp.presenter.Imp.UserLogPresenterImp;
import com.example.yangzi.mvp_dev.mvp.presenter.UserLogPresenter;
import com.example.yangzi.mvp_dev.mvp.view.UserLogView;
import com.example.yangzi.mvp_dev.utils.LogUtils;
import com.example.yangzi.mvp_dev.utils.ProgressUtils;
import com.example.yangzi.mvp_dev.utils.RegexUtils;
import com.example.yangzi.mvp_dev.utils.SnackbarUtils;
import com.example.yangzi.mvp_dev.utils.ToastUtils;
import com.example.yangzi.mvp_dev.widget.OwlView;
import com.example.yangzi.mvp_dev.widget.VerifyCodeButton;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by zhouas666 on 2017/12/8.
 */
public class VerifyPhoneActivity extends BaseActivity implements UserLogView {
    String phonenum;
    boolean isphoneExisted= false;
    @BindView(R.id.verify_et_phone)
    TextInputLayout phone;
    @BindView(R.id.verify_text)
    TextView phonetext;

    @BindView(R.id.verify_get_code)
    TextInputLayout getCode;
    @BindView(R.id.btn_verify_get_code)
    VerifyCodeButton getcodeBtn;
    @BindView(R.id.verify_btn_verify)
    Button loginBtn;
    private EditText mEdtPhone;
    private EditText mEdtCode;

    @Override
    protected int getLayout() {
        return R.layout.activity_verify_phone;
    }


    @Override
    protected void initEventAndData() {

        phone.setVisibility(View.GONE);
        mEdtPhone = phone.getEditText();
        Intent intent = getIntent();
        phonenum = intent.getStringExtra("phoneNumber");
        if (intent.getStringExtra("index").equals("1001")) {
            phone.setVisibility(View.VISIBLE); //如果是修改手機號,驗證新輸入的手機號
        } else {
            phonetext.setText("(" + phonenum.substring(0, 3) + "****" + phonenum.substring(7, 11) + ")"); //如果是其他,驗證傳進的手機號
        }
        mEdtCode = getCode.getEditText();
        mEdtCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String code = charSequence.toString();
                if (!RegexUtils.checkCode(code)) {
                    getCode.setError("請輸入正確的驗證碼");
                } else {
                    getCode.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    /**
     * 監聽點選事件
     *
     * @param view
     */
    @OnClick({R.id.btn_verify_get_code, R.id.verify_btn_verify})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_verify_get_code: // 獲取驗證碼

                getCode();
                break;
            case R.id.verify_btn_verify:  //button
                    verify();  //登陸
                break;
            default:
                break;
        }
    }

    private void getCode() {
        if(phone.isShown())  phonenum = mEdtPhone.getText().toString();
        requestSMScodeFromBmob(phonenum);
     }

    /**
     * 請求驗證碼
     * @param phone_number 手機號
     * @param template_name 驗證碼模板名稱
     * @param listener 回撥
     */
    /**
     * TODO template 如果是自定義簡訊模板,此處替換為你在控制檯設定的自定義簡訊模板名稱;如果沒有對應的自定義簡訊模板,則使用預設簡訊模板。
     */
    public void requestSMScodeFromBmob(String phone) {

        getcodeBtn.start();
        Log.v(LogUtils.filename(new Exception()),LogUtils.funAndLine(new Exception())+phone);
        BmobSMS.requestSMSCode(phone, "register", new QueryListener<Integer>() {
            @Override
            public void done(Integer smsId, BmobException e) {
                if (e == null) {
                    Toast.makeText(mContext, "傳送驗證碼成功,簡訊ID:" + smsId + "\n", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "傳送驗證碼失敗:" + e.getErrorCode() + "-" + e.getMessage() + "\n", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 執行驗證動作
     */
    public void verify() {
        if(phone.isShown())  phonenum = mEdtPhone.getText().toString();
        String code = mEdtCode.getText().toString();
        Log.v(LogUtils.filename(new Exception()),LogUtils.funAndLine(new Exception())+phonenum);
        veriflySMSCode(phonenum,code);
    }



    private void veriflySMSCode(final String phonenumber,final String code) {
        B