package com.example.day_09;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText edit_name,edit_pass;
private CheckBox check_remember,check_motion;
private Button button;
private SharedPreferences sharedPreferences;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取資源id
findViewById();
//
sharedPreferences=getSharedPreferences("user", MODE_PRIVATE);
editor=sharedPreferences.edit();
//取出記住密碼的狀態值
boolean r_ischeck = sharedPreferences.getBoolean("r_ischeck", false);
//判斷是否為選中
if (r_ischeck) {
String name = sharedPreferences.getString("name", null);
String pass = sharedPreferences.getString("pass", null);
//賦值
edit_name.setText(name);
edit_pass.setText(pass);
check_remember.setChecked(true);
}
//取出自動登入的狀態值
boolean v_ischecked = sharedPreferences.getBoolean("v_ischecked", false);
if (v_ischecked) {
Intent intent = new Intent(MainActivity.this, LoginAction.class);
startActivity(intent);
//銷燬
finish();
}
//點選自動登入監聽
check_motion.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
check_remember.setChecked(true);
}else{
check_remember.setChecked(false);
}
}
});
//點選button監聽
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name=edit_name.getText().toString();
String pass = edit_pass.getText().toString();
//判斷記住密碼是否勾選
if (check_remember.isChecked()) {
//存值
editor.putString("name", name);
editor.putString("pass", pass);
editor.putBoolean("r_ischeck", true);
//提交
editor.commit();
}else{
//登出
editor.clear();
//提交
editor.commit();
}
//判斷自動登入是否勾選
if (check_motion.isChecked()) {
editor.putBoolean("v_ischecked", true);
//提交
editor.commit();
}
//判斷不為空
if (name.equals("")&&pass.equals("")) {
Toast.makeText(MainActivity.this, "使用者名稱和密碼不能為空", 1).show();
}else{
//跳轉
Intent intent = new Intent(MainActivity.this, LoginAction.class);
startActivity(intent);
//銷燬
finish();
}
}
});
}
//獲取資源id
public void findViewById(){
edit_name=(EditText) findViewById(R.id.edit_name);
edit_pass=(EditText) findViewById(R.id.edit_pass);
check_remember=(CheckBox) findViewById(R.id.check_remember);
check_motion=(CheckBox) findViewById(R.id.check_motion);
button=(Button) findViewById(R.id.button1);
}
}