版权声明:李帅哲专属 https://blog.csdn.net/weixin_43584282/article/details/84259047
//布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:hint="请输入账号"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/edit_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_name"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:hint="请输入密码"
android:ems="10" />
<CheckBox
android:id="@+id/check_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_pass"
android:layout_below="@+id/edit_pass"
android:layout_marginTop="29dp"
android:text="记住密码" />
<CheckBox
android:id="@+id/check_voluntarily"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/check_remember"
android:layout_alignBottom="@+id/check_remember"
android:layout_alignRight="@+id/edit_pass"
android:text="自动登录" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/check_remember"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="登录" />
</RelativeLayout>
//main主界面操作
package com.example.day09_sharedpreferences_demo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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;
public class MainActivity extends Activity {
private SharedPreferences sharedPreferences;
private Editor editor;
private EditText edit_name,edit_pass;
private CheckBox check_remember,check_voluntarily;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.获取资源ID
findByID();
//2.得到sharedPreferences
sharedPreferences = getSharedPreferences("User", MODE_PRIVATE);
//得到editor
editor = sharedPreferences.edit();
//7.取出记住密码的状态值进行判断
boolean r_ischeck = sharedPreferences.getBoolean("r_ischeck", false);
//8.判断状态值 如果为true就记住密码,如果为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_ischeck = sharedPreferences.getBoolean("v_ischeck", false);
if(v_ischeck){
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
//勾选自动登录同时记住密码
check_voluntarily.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
//设置记住密码为勾选
check_remember.setChecked(true);
}else{
//清空
editor.clear();
editor.commit();
}
}
});
//3.点击按钮将值存入sharedPreferences
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//4.判断记住密码是否有没有勾选
if(check_remember.isChecked()){
//5.获取输入框的值
String name = edit_name.getText().toString();
String pass = edit_pass.getText().toString();
//6.将值存入sharedPreferences
editor.putString("name", name);
editor.putString("pass", pass);
//存入一个勾选了的状态值
editor.putBoolean("r_ischeck", true);
//提交
editor.commit();
}else{
//清空
editor.clear();
editor.commit();
}
//自动登录
if(check_voluntarily.isChecked()){
editor.putBoolean("v_ischeck", true);
editor.commit();
}
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
});
}
public void findByID(){
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_voluntarily = (CheckBox) findViewById(R.id.check_voluntarily);
button = (Button) findViewById(R.id.button1);
}
}
//登录成功进入 页面布局 (点击注销)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录成功" />
<Button
android:id="@+id/l_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="182dp"
android:text="注销" />
</RelativeLayout>
//注销代码操作
package com.example.day09_sharedpreferences_demo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LoginActivity extends Activity {
private SharedPreferences sharedPreferences;
private Editor editor;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//获取资源ID
button = (Button) findViewById(R.id.l_but);
sharedPreferences = getSharedPreferences("User", MODE_PRIVATE);
editor = sharedPreferences.edit();
//点击注销返回
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//清空
editor.clear();
editor.commit();
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}