理论知识
一、SharedPreferences:
1.是一种轻型的数据存储方式
2.本质是基于XML文件存储Key-Value键值对数据
3.通常用来存储一些简单的配置信息
存储的位置在APP的data目录下
1.SharedPreferences对象本身只能获取数据而不支持存储和修改,存储和修改,存储和修改是通过Editor对象实现。
2.实现SharedPreferences存储的步骤如下:
(1)获得SharedPreferences对象
(2)获得SharedPreferences.Editor对象
(3)通过Editor接口和putXxx方法保存Key—Value对,其中Xxx表示不同的数据类型
(4)通过Editor接口的commit方法保存Key—Value对
布局文件——
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/user_name"
android:layout_width="200dp"
android:singleLine="true"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:text="密 码:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/user_password"
android:layout_width="200dp"
android:singleLine="true"
android:layout_height="wrap_content" />
</LinearLayout>
<CheckBox
android:text="保存用户名"
android:id="@+id/save_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
程序代码——
package com.example.administrator.teachdemo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText etUserName,etUserPass;
CheckBox chk;
SharedPreferences pref;
SharedPreferences.Editor editor;
Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
// basicSet();
plusSet();
}
private void init() {
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etUserName.getText().toString().trim();
String password = etUserPass.getText().toString().trim();
//保存用户名
if("admin".equals(name) && "123456".equals(password)){
if(chk.isChecked()){
editor.putString("userName", name);
editor.commit(); //调用才算提交成功
}else {//如果没勾选
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
editor.remove("userName");
editor.commit();
}
}else {
Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
}
});
}
private void plusSet() {
etUserName = (EditText) findViewById(R.id.user_name);
etUserPass = (EditText) findViewById(R.id.user_password);
chk = (CheckBox) findViewById(R.id.save_name);
pref = getSharedPreferences("UserInfo",MODE_PRIVATE);
editor = pref.edit();
//取本地存储的值
String name = pref.getString("userName",""); //获取的值,值的默认值
if(name == null){
chk.setChecked(false);
}else {
chk.setChecked(true);
etUserName.setText(name);
}
}
/**
* SharedPreferences的基本使用:创建,添加参数,提交,获取
*/
private void basicSet() {
//方法一
// SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
//方法二
SharedPreferences pref = getSharedPreferences("myPref",MODE_PRIVATE);
//获得编辑器对象
SharedPreferences.Editor editor = pref.edit();
editor.putString("name", "Lina");
editor.putInt("age", 30);
editor.putLong("time", System.currentTimeMillis());
editor.putBoolean("default", true);
editor.commit(); //提交数据
editor.remove("default");
editor.commit();
//取值
Log.e("取值",""+pref.getString("name",""));
Log.e("取值",""+pref.getInt("age", 0));
}
}