小编初学安卓,对sharedpreferences的学习有了一定的了解,有了很多体会,希望用下面的代码展示我的学习成果,也希望能给初学安卓的童鞋一些指引,当然这里也有很多不足之处,还望大家多批评指正!给予我更多的支持和鼓励。话不多说,代码抵千言
1.主页面的lanout布局文件,很简单就直接见下面的布局文件代码:
<LinearLayout 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"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff55"
android:gravity="center"
android:padding="5dp"
android:text="登陆"
android:textSize="20sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:padding="5dp"
android:textSize="20sp" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户密码"
android:padding="5dp"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="login"
/>
</LinearLayout>
2.主页面的代码逻辑,代码如下:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
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 et_name;
private EditText et_pwd;
private CheckBox checkbox1;
private CheckBox checkbox2;
private SharedPreferences sf;
private String name;
private String password;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到相对应的控件
et_name = (EditText) findViewById(R.id.et_name);
et_pwd = (EditText) findViewById(R.id.et_pwd);
checkbox1 = (CheckBox) findViewById(R.id.checkbox1);
checkbox2 = (CheckBox) findViewById(R.id.checkbox2);
// 获得sharePreference的对象
sf = getSharedPreferences("config", MODE_PRIVATE);
// 获得存值的编辑器
editor = sf.edit();
// 取出我们存取的用户名和密码的值
String name = sf.getString("name", "");
String password = sf.getString("password", "");
// 取值
boolean flagPwd = sf.getBoolean("rememberPwd", false);
// 进行判断 是否是记住密码了
if (flagPwd) {
// 进行非空的判断 不为空的话就把取出来的值设置到文本编辑框里面
if (!TextUtils.isEmpty(name)) {
et_name.setText(name);
}
if (!TextUtils.isEmpty(password)) {
et_pwd.setText(password);
}
} else {
// 不做处理
}
// checkBox记住密码的监听事件
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// 判断的勾选的状态 勾选和不勾选 2种情况
if (isChecked) {
editor.putBoolean("rememberPwd", true);
} else {
editor.putBoolean("rememberPwd", false);
}
// 提交
editor.commit();
}
});
// checkBox自动登陆的监听事件
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// 判断的勾选的状态 勾选和不勾选 2种情况
if (isChecked) {
editor.putBoolean("autologin", true);
} else {
editor.putBoolean("autologin", false);
}
// 提交
editor.commit();
}
});
// 取我们存取的boolean的值
boolean flag = sf.getBoolean("autologin", false);
// 进行判断 如果取到了自动登陆的值 就直接登陆跳转到登陆界面
if (flag) {
// 睡眠操作 展示效果明显
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 页面跳转操作
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
// 结束当前界面
finish();
} else {
// 不做处理
}
}
// 登陆的监听事件
public void login(View v) {
// 得到编辑框里面的值
name = et_name.getText().toString().trim();
password = et_pwd.getText().toString().trim();
// 登陆前的判断 用户名和密码都不为空的情况 才可以登陆成功
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
// 登陆前 我们进行存值操作 存入姓名和密码的值
editor.putString("name", name);
editor.putString("password", password);
// 存完值后 需要提交 才能存值成功
editor.commit();
// 登陆前进行睡眠 效果明显点
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 页面跳转操作
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
// 结束当前界面
finish();
} else {
// 吐司,给用户提示
Toast.makeText(this, "用户名和密码不能为空!", 0).show();
}
}
}
3.登陆界面的布局文件,页面只是一个TextView显示登陆成功的展示:
<?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="#00ff99"
android:orientation="vertical" >
<TextView
android:layout_marginTop="200dp"
android:gravity="center"
android:textSize="20sp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="恭喜登陆成功!" />
</LinearLayout>
4.代码结束,谢谢大家!