大家好,相信各位都有在一般常見的登入畫面上看到勾選記住密碼的選項,是的,在之前我們實作了註冊、登入的功能,而今天會使用checkbox來實作記住密碼的功能。
checkbox允許用戶可以選取一個至多個的選項,每一個checkbox是獨立操作的,不會互相影響,舉例來說:假如有三個checkbox,就必須將這三個checkbox各別設立三個監聽事件。
首先在activity_main.xml新增一個checkbox
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:text="記住密碼" />
MainActivity程式設計
package com.example.test1;
import ...
public class MainActivity extends AppCompatActivity {
private EditText useraccount,userpassword;
private Button userregister,signin;
private CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//取得元件的id
components();
//設定button的監聽事件
buttonsetting();
checkBoxset();
}
//取得元件的id
private void components() {
useraccount=(EditText)findViewById(R.id.useraccount);
userpassword=(EditText)findViewById(R.id.userpassword);
userregister=(Button)findViewById(R.id.userregister);
signin=(Button)findViewById(R.id.signin);
checkBox=(CheckBox)findViewById(R.id.checkbox);
}
//設定button的監聽事件
private void buttonsetting() {
userregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent main = new Intent(MainActivity.this, Register.class);
startActivity(main);
}
});
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userid = useraccount.getText().toString();
String userpasswd = userpassword.getText().toString();
//取得sharedpreference
SharedPreferences preference = getSharedPreferences("data", MODE_PRIVATE);
//判斷登入畫面輸入的帳號密碼是否跟註冊的帳號密碼一樣
if (userid.equals(preference.getString("id", ""))
&& userpasswd.equals(preference.getString("password", ""))) {
checkBoxing();
//是的話顯示成功登入
Toast.makeText(MainActivity.this, "成功登入", Toast.LENGTH_SHORT).show();
} else {
//不是則顯示登入失敗
Toast.makeText(MainActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
}
}
});
}
private void checkBoxing() {
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences=getSharedPreferences("Check",MODE_PRIVATE);
SharedPreferences.Editor edit=sharedPreferences.edit();
if (checkBox.isChecked()){
edit.putString("account",useraccount.getText().toString());
edit.putString("passwd",userpassword.getText().toString());
edit.putBoolean("Check",true);
}else{
edit.remove("account");
edit.remove("passwd");
edit.putBoolean("Check",false);
}
edit.apply();
}
});
}
private void checkBoxset() {
SharedPreferences sharedPreferences=getSharedPreferences("Check",MODE_PRIVATE);
boolean Check =sharedPreferences.getBoolean("Check",false);
String account = sharedPreferences.getString("account", "");
String passwd = sharedPreferences.getString("passwd", "");
useraccount.setText(account);
userpassword.setText(passwd);
checkBox.setChecked(Check);
}
}
假如登入的帳號跟密碼與註冊的帳密一樣,會執行在checkBoxing()的checkBox的點擊事件。
private void checkBoxing() {
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//新增一個SharedPreferences,並開啟編輯
SharedPreferences sharedPreferences=getSharedPreferences("Check",MODE_PRIVATE);
SharedPreferences.Editor edit=sharedPreferences.edit();
//如果checkBox被勾選,儲存登入的帳密,並將Check設置為true
if (checkBox.isChecked()){
edit.putString("account",useraccount.getText().toString());
edit.putString("passwd",userpassword.getText().toString());
edit.putBoolean("Check",true);
}
edit.apply();
}
});
}
//判斷記住密碼的狀態
private void checkBoxset() {
//取得sharedPreferences
SharedPreferences sharedPreferences=getSharedPreferences("Check",MODE_PRIVATE);
//設定Check如果打勾則為true,若沒有則為false
boolean Check =sharedPreferences.getBoolean("Check",false);
//取得上次輸入的帳密,並顯示出來
String account = sharedPreferences.getString("account", "");
String passwd = sharedPreferences.getString("passwd", "");
useraccount.setText(account);
userpassword.setText(passwd);
checkBox.setChecked(Check);
}