SharedPreferencs
用来存储一下轻量级的数据:用户名、密码等,可以被本程序的其他activity共享。
一、存储数据类型有:Boolean,Float,Int,Long,String.
二、数据存储路径:android文件系统目录的/data/data/PAACKAGE_NAME/shared_prefs下的XML文件中
三、数据存储过程
1)通过getSharedPreferences()方法获取SharedPreferences对象
方法: getSharedPreferences(“Content”,Context.MODE_PRIVATE);
2)创建Editor编辑器
方法: getSharedPreferences.edit();
3)使用Editor修改内容
方法: putString("String",data);
4)保存数据
方法:editor.commit();
四、数据读取过程
1)获取SharedPreferences对象
方法: getSharedPreferences(“Content”,Context.MODE_PRIVATE);
2)取得Key对应的Value
方法:SharedPreferences.getString();
SharedPreferences.getBoolean();
SharedPreferences.getInt();等方法。
五、实例
1)描述:在用户名和密码编辑框输入数据,点击注册,数据就会保存进SharedPreferences并清空编辑框,点击登录就会读取SharedPreferences中保存好的数据并显示在编辑框中。
2)实现代码
布局
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:">
</TextView>
<EditText
android:id="@+id/editText1"
android:layout_width="250dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout1"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码: ">
</TextView>
<EditText
android:id="@+id/editText2"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout2"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginLeft="60dip"
android:layout_marginRight="5dip"
android:text="登陆"
android:textSize="20dip"
android:background="#87CEEB"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="5dip"
android:text="注册"
android:textSize="20dip"
android:background="#87CEEB"
/>
</LinearLayout>
</RelativeLayout>
主代码
package com.example.sharedpreferencesdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
public static final String SETTING_INFOS = "SETTING_Infos";
public static String name="";
public static String password="";
private EditText edittext1;
private EditText edittext2;
private Button button01;
private Button button02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
edittext1=(EditText)findViewById(R.id.editText1);
edittext2=(EditText)findViewById(R.id.editText2);
button01=(Button)findViewById(R.id.button1);
button02=(Button)findViewById(R.id.button2);
edittext1.setOnClickListener(this);
edittext2.setOnClickListener(this);
button01.setOnClickListener(this);
button02.setOnClickListener(this);
//name = edittext1.getText().toString();
//password = edittext2.getText().toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
getData();
break;
case R.id.button2:
onStop();
break;
default:
break;
}
}
protected void getData(){
//获取一个SharedPreferences对象,SETTING_INFOS与SETTING_INFOS.XML文件相对应
//效果相当于读取SETTING_INFOS.XML文件,如果文件不存在,则进行创建
SharedPreferences sharedPreferences = getSharedPreferences(SETTING_INFOS, 0);
//取出保存的name,取出改字段名的值,不存在则创建默认为空
String userName=sharedPreferences.getString(name, "");
//取出保存的password,取出改字段名的值,不存在则创建默认为空
String userPassword=sharedPreferences.getString(password,"");
//提示成功获取数据
Toast.makeText(this, "获取数据成功 ", Toast.LENGTH_LONG).show();
//将取出来的用户名赋予edittext1
edittext1.setText(userName);
//将取出来的密码赋予edittext2
edittext2.setText(userPassword);
}
protected void onStop(){
super.onStop();
//打开SETTING_INFOS.XML
//0表示打开模式,有则读,没有则创建
SharedPreferences sharedPreferences = getSharedPreferences(SETTING_INFOS, 0);
//下面一句的语法比较神奇,每次执行都返回一个sharedPreferences.edit(),commit()表示执行
//将edittext1赋值给name字段,将edittext2赋值给password字段
sharedPreferences.edit()
.putString(name,edittext2.getText().toString())
.putString(password,edittext1.getText().toString()).commit();
//提示保存数据成功
Toast.makeText(this, "保存数据成功 ", Toast.LENGTH_LONG).show();
//清空编辑框
edittext1.setText("");
edittext2.setText("");
}
}
3)效果图(由于点击注册时编辑框清空太快无法截图)