[Android]数据篇 --- SharedPreferences

时间:2023-03-08 20:08:46

转载请标注:转载于http://www.cnblogs.com/Liuyt-61/p/6637515.html

---------------------------------------------------------------

Android数据的四种存储方式:

      1、SharedPreferences

      2、SQLite

      3、Content Provider

      4、File

----------------------分割线----------------------------------

一、SharedPreferences:

    1.是一种轻型数据存储方式.

    2.本质是基于XML文件存储 key-value 键值对数据

    3.通常用来存储一些简单的配置信息,如用户名、密码(后面附上实例代码)

1>SharedPreferences对象本身只能获取数据而不支持存储和修改

  Editor实现存储和修改

2>实现存储的步骤:

  ①使用Activity类的getSharedPreferences获取其对象,其中存储key-value的文件的名称由getSharedPreferences方法第一个参数指定。

  ②使用SharedPreferences接口的Edit获得SharedPreferences.Editor对象。

  ③通过SharedPreferences.Editor接口的put×××方法保存key-value对

  ④通过SharedPreferences.Editor接口的commit()方法保存key-value对进行提交。

直接上实例代码(登录界面保存用户名)

main.xml

<?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:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" /> <EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:ems=""
android:hint="input username"
android:inputType="textPersonName" >
</EditText>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码" /> <EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:ems=""
android:hint="input password"
android:inputType="textPassword" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <CheckBox
android:id="@+id/cb_remember"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="登录" /> <Button
android:id="@+id/btn_canel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="取消" />
</LinearLayout> </LinearLayout>

MainActivity.java

package com.Liuyt.s03_e28_sharedpreferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
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 {
private Button btn_login,btn_canel;
private EditText et_username,et_password;
private CheckBox cb_remenber;
private Editor editor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);
// Editor editor = pref.edit();
// editor.putString("name", "Liuyt");
// editor.putInt("age",18);
// editor.commit();
// System.out.println(pref.getString("name", ""));
// System.out.println(pref.getInt("age", 0));
btn_canel = (Button) findViewById(R.id.btn_canel);
btn_login = (Button) findViewById(R.id.btn_login);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb_remenber = (CheckBox) findViewById(R.id.cb_remember);
SharedPreferences pref = getSharedPreferences("myPref1", MODE_PRIVATE);
editor = pref.edit();
String name = pref.getString("username", "");
if(name == null){
cb_remenber.setChecked(false);
}else{
cb_remenber.setChecked(true);
et_username.setText(name);
}
}
public void doClick(View view){
switch (view.getId()) {
case R.id.btn_login:
String name = et_username.getText().toString().trim();//去掉首尾空格
String password = et_password.getText().toString().trim();
if("admin".equals(name)&&"".equals(password)){
if(cb_remenber.isChecked()){
editor.putString("username", name);
editor.commit();
}else{
editor.remove("username");
editor.commit();
}
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "拒绝登录", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
} }