《Android項目實戰-博學谷》設置密保&找回密碼

設置密保&找回密碼界面

思路

因爲設置密保和找回密碼兩個界面十分類似,代碼邏輯也十分類似,所以這兩個界面能夠使用同一個佈局文件和同一個
Activity 來處理,首先導入所需圖片資源文件,再建立 FindPswActivity ,將佈局改成 LinearLayout java

具體代碼以下:android

activity_find_psw.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:background="@drawable/login_bg"
    android:orientation="vertical">

    <include layout="@layout/main_title_bar" />

    <!--先將設置密保部分隱藏,android:visibility="gone"-->

    <TextView
        android:id="@+id/tv_user_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="35dp"
        android:text="您的用戶名是?"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:visibility="gone" />

    <EditText
        android:id="@+id/et_user_name"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/find_psw_icon"
        android:hint="請輸入您的用戶名"
        android:paddingLeft="8dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3"
        android:visibility="gone" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:text="您的姓名是?"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/et_validate_name"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/find_psw_icon"
        android:hint="請輸入要驗證的姓名"
        android:paddingLeft="8dp"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#a3a3a3" />

    <TextView
        android:id="@+id/tv_reset_psw"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:visibility="gone" />

    <Button
        android:id="@+id/btn_validate"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/register_selector"
        android:text="驗證"
        android:textColor="@android:color/white"
        android:textSize="18sp" />
</LinearLayout>

設置密保&找回密碼邏輯

思路

因爲設置密保界面和找回密碼界面用的同一個 Activity ,在這個Activity中主要是根據從設置界面和登陸界面
putExtra 傳遞過來的 from 參數的值來判斷要跳轉到哪一個界面,若值爲 security 則是設置密保的界面,不然處理的就是找回密碼的界面,完成設置密保&找回密碼邏輯後再處理設置界面與登陸界面的點擊事件app

具體代碼以下:編輯器

FindPswActivity

package cn.edu.lt.android.boxueguapp.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.lt.android.boxueguapp.R;
import cn.edu.lt.android.boxueguapp.utils.AnalysisUtils;
import cn.edu.lt.android.boxueguapp.utils.MD5Utils;

public class FindPswActivity extends AppCompatActivity {
    private EditText et_validate_name, et_user_name;
    private Button btn_validate;
    private TextView tv_main_title;
    private TextView tv_back;
    //從哪一個界面跳轉過來的(from爲security時是從設置密保界面跳轉過來的,不然就是從登陸界面跳轉過來的)
    private String from;
    private TextView tv_reset_psw, tv_user_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw);
        //設置此界面爲豎屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //獲取從登陸界面和設置界面傳遞過來的數據
        from = getIntent().getStringExtra("from");
        init();
    }

    /**
     * 獲取界面控件及處理相應控件的點擊事件
     */
    private void init() {
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_back = (TextView) findViewById(R.id.tv_back);
        et_validate_name = (EditText) findViewById(R.id.et_validate_name);
        btn_validate = (Button) findViewById(R.id.btn_validate);
        tv_reset_psw = (TextView) findViewById(R.id.tv_reset_psw);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        tv_user_name = (TextView) findViewById(R.id.tv_user_name);
        if ("security".equals(from)) {
            tv_main_title.setText("設置密保");
        } else {
            tv_main_title.setText("找回密碼");
            //將隱藏的控件顯示出來
            tv_user_name.setVisibility(View.VISIBLE);
            et_user_name.setVisibility(View.VISIBLE);
        }
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindPswActivity.this.finish();
            }
        });
        btn_validate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String validateName = et_validate_name.getText().toString().trim();
                if ("security".equals(from)) {//設置密保
                    if (TextUtils.isEmpty(validateName)) {
                        Toast.makeText(FindPswActivity.this, "請輸入要驗證的姓名", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        Toast.makeText(FindPswActivity.this, "密保設置成功", Toast.LENGTH_SHORT).show();
                        //保存密保到SharedPreferences
                        saveSecurity(validateName);
                        FindPswActivity.this.finish();
                    }
                } else {//找回密碼
                    String userName = et_user_name.getText().toString().trim();
                    String sp_security = readSecurity(userName);
                    if (TextUtils.isEmpty(userName)) {
                        Toast.makeText(FindPswActivity.this, "請輸入您的用戶名", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (!isExistUserName(userName)) {
                        Toast.makeText(FindPswActivity.this, "您輸入的用戶名不存在", Toast.LENGTH_SHORT).show();
                        return;
                    } else if (TextUtils.isEmpty(validateName)) {
                        Toast.makeText(FindPswActivity.this, "請輸入要驗證的姓名", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (!validateName.equals(sp_security)) {
                        Toast.makeText(FindPswActivity.this, "輸入的密保不正確", Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        //輸入的密保正確,從新給用戶設置一個密碼
                        tv_reset_psw.setVisibility(View.VISIBLE);
                        tv_reset_psw.setText("初始密碼:123456");
                        savePsw(userName);
                    }
                }
            }
        });
    }

    /**
     * 保存初始化密碼
     * @param userName
     */
    private void savePsw(String userName) {
        String md5Psw = MD5Utils.md5("123456");//把密碼用Md5加密
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);//loginInfo表示文件名
        SharedPreferences.Editor editor = sp.edit();//獲取編輯器
        editor.putString(userName, md5Psw);
        editor.commit();//提交修改
    }

    /**
     * 保存密保到SharedPreferences中
     */
    private void saveSecurity(String validateName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);//loginInfo表示文件名
        SharedPreferences.Editor editor = sp.edit();//獲取編輯器
        editor.putString(AnalysisUtils.readLoginUserName(this) + "_security", validateName);//存入帳號對應的密保
        editor.commit();//提交修改
    }

    /**
     * 從SharedPreferences中讀取密保
     */
    private String readSecurity(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        String security = sp.getString(userName + "_security", "");
        return security;
    }

    /**
     * 從SharedPreferences中根據用戶輸入的用戶名來判斷是否有此用戶名
     */
    private boolean isExistUserName(String userName) {
        boolean hasUserName = false;
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw = sp.getString(userName, "");
        if (!TextUtils.isEmpty(spPsw)) {
            hasUserName = true;
        }
        return hasUserName;
    }
}

LoginActivity

//找回密碼控件的點擊事件
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳轉找回密碼界面
                Intent intent=new Intent(LoginActivity.this,FindPswActivity.class);
                startActivity(intent);
            }
        });

SettingActivity

//設置密保的點擊事件
        rl_security_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, FindPswActivity.class);
                intent.putExtra("from", "security");
                startActivity(intent);
            }
        });

優化

思路

爲了界面美觀以及操做的便捷,找回密碼點擊驗證後,本須要手動回退到登陸界面,現本人加入倒計時,倒計時截止時將自動跳轉登陸界面ide

FindPswActivity

private int countdown = 3;
//輸入的密保正確,從新給用戶設置一個密碼
                        tv_reset_psw.setVisibility(View.VISIBLE);
                        tv_reset_psw.setTextColor(0xffff0000);
                        tv_reset_psw.setText("初始密碼爲123456");
                        tv_countdown.setVisibility(View.VISIBLE);
                        tv_countdown.setAlpha(0.5f);
                        tv_countdown.setText("此界面將在"+countdown+"秒後跳轉");
                        savePsw(userName);
                        //倒計時跳轉
                        final Timer timer = new Timer();
                        TimerTask timerTask = new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {      // UI thread
                                    @Override
                                    public void run() {
                                        countdown--;
                                        tv_countdown.setVisibility(View.VISIBLE);
                                        tv_countdown.setText("此界面將在"+countdown+"秒後跳轉");
                                        if(countdown == 0){
                                            timer.cancel();
                                            Intent intent = new Intent(FindPswActivity.this, LoginActivity.class);
                                            startActivity(intent);
                                            FindPswActivity.this.finish();//跳轉後關閉當前頁面
                                        }
                                    }
                                });

                            }
                        };

                        timer.schedule(timerTask,1000,1000);

運行效果

運行效果1

運行效果2

相關文章
相關標籤/搜索