您的位置:首页 > 移动开发 > Android开发

Android 之数据存储--SharedPreferences

2015-09-07 21:47 399 查看
在android中,谷歌为我们提供了三种数据持久化保存的三种方案。

SharedPreferences:SharedPreferences
对象指向了一个保存key-value pairs的文件,并且它提供了简单的方法来读写这个文件。

文件存储:文件存储是
Android 中最基本的一种数据存储方式,它不对存储的内容进行任何的格式化处理,所有数据都是原封不动地保存到文件当中的,因而它比较适合用于存储一些简单的文本数据或二进制数据。

SQLite 数据库存储:andorid内置的一个轻量级数据库,你可以sql语法来对数据进行较复杂的增删改查,一般适用于需要存储较复杂的数据结构。

这一节我们主要来讲解sharedPreference的使用方法 。

为了获得sharedPreference的持久化文件,android为我们提供了一些方法来创建该及操作持久化文件

要想使用SharedPreferences来存储数据, 首先需要获取到SharedPreferences对象。 Android中主要提供了三种方法用于得到 SharedPreferences 对象。

1. Context 类中的 getSharedPreferences()方法

此方法接收两个参数,第一个参数用于指定 SharedPreferences文件的名称,如果指定的文件不存在则会创建一个,SharedPreferences 文件都是存放在/data/data/<packagename>/shared_prefs/目录下的。第二个参数用于指定操作模式,主要有两种模式可以选择,MODE_PRIVATE 和 MODE_MULTI_PROCESS。MODE_PRIVATE
仍然是默认的操作模式,和直接传入 0 效果是相同的,表示只有当前的应用程序才可以个SharedPreferences文件进行读写。 MODE_MULTI_PROCESS则一般是用于会有多个进程中对同一个 SharedPreferences文件进行读写的情况。类似地,MODE_WORLD_READABLE和 MODE_WORLD_WRITEABLE 这两种模式已在 Android 4.2 版本中被废弃。

2. Activity 类中的 getPreferences()方法

这个方法和 Context 中的 getSharedPreferences()方法很相似,不过它只接收一个操作模式参数,因为使用这个方法时会自动将当前活动的类名作为 SharedPreferences 的文件名。
3. PreferenceManager 类中的 getDefaultSharedPreferences()方法

这是一个静态方法,它接收一个 Context 参数,并自动使用当前应用程序的包名作为前缀来命名 SharedPreferences 文件。

接下来我们就用一个实例来进行试验,实现一个记住密码及用户名的小应用。新建一个SharedPreferencesTest工程。

下面是登陆界面的逻辑,新建一个loginActivity类,添加如下代码:


import android.widget.Toast;
public class loginActivity extends Activity implements OnClickListener{

private SharedPreferences share ;

private SharedPreferences.Editor edit ;

private CheckBox check ;

private EditText username ;

private EditText password ;

private Button send ;

@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated
method stub
super.onCreate(savedInstanceState);
setContentView(R.layout. main);
share=PreferenceManager.getDefaultSharedPreferences(this);//实例化SharedPreferences对象
check=(CheckBox)
findViewById(R.id. remember);
username=(EditText)
findViewById(R.id. login_text);
password=(EditText)
findViewById(R.id. login_password_text);
send=(Button)
findViewById(R.id. send);

boolean judge=share .getBoolean("remember_password", false);//获取judge中的值,如果没有值则默认为false

if(judge){
String username_text= share.getString( "username", "" );
String password_text= share.getString( "password", "" );
username.setText(username_text);
password.setText(password_text);
check.setChecked( true);
}
send.setOnClickListener( this);
}
@Override
public void onClick(View
v) {
// TODO Auto-generated
method stub
switch (v.getId())
{
case R.id.send :{
String username_text= username.getText().toString();
String password_text= password.getText().toString();
if(username_text.equals("admin" )&&password_text.equals("123456")){
edit= share.edit();//实例化SharedPreferences的操作对象,使他可以操作数据的增删改查
if(check .isChecked()){
edit.putBoolean( "remember_password", true );//添加数据
edit.putString( "username",
username_text);
edit.putString( "password",
username_text);
} else{
edit.clear();//如果checkbox没有打钩,则进行消除数据
}
edit.apply();
Intent intent = new Intent(loginActivity.this ,MainActivity.class);
startActivity(intent);
finish();
} else{
Toast. makeText(MyActivity.this, "account
or password is invalid", Toast.LENGTH_SHORT).show();
}
break;
}
default:
break;
}
}
以下是main.xml的代码:
<?xml version= "1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login"
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/login_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="用户名"
android:textSize="15sp" />

<EditText
android:id="@+id/login_text"
android:layout_width="0dp"
android:hint="用户名"
android:layout_height="wrap_content"
android:layout_weight="1"
android:lines="1" />
</LinearLayout >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/login_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:minLines="1"
android:padding="10dp"
android:text="密码"
android:textSize="15sp" />

<EditText
android:id="@+id/login_password_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="密码"
android:inputType="textPassword"
android:lines="1" />
</LinearLayout >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />

<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陸" />
</LinearLayout >

</LinearLayout>
再在AndroidManifest.xml中将loginActivity添加进去即可看到运行结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: