在andriod数据存储四中讲到了对文件的保存和读取,在android数据存储五中来了解如何保存用户的配置信息。那就先来了解一个类sharedPreferences,它主要用来保存软件的配置信息。
下面就用一个Demo来看一下:
SharedPreferencesActivity.java代码如下:
package com.cooguo.activity;
import java.util.jar.Attributes.Name;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SharedPreferencesActivity extends Activity {
private Button btnSave = null;
private Button btnRead = null;
private EditText editName = null;
private EditText editAge = null;
private TextView textResult = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSave = (Button)findViewById(R.id.btnSave);
btnRead = (Button)findViewById(R.id.btnShow);
editName = (EditText)findViewById(R.id.editName);
editAge = (EditText)findViewById(R.id.editAge);
textResult = (TextView)findViewById(R.id.textResult);
btnSave.setOnClickListener(listener);
btnRead.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener(){
@Override
public void onClick(View view) {
SharedPreferences pres = SharedPreferencesActivity.this.getSharedPreferences("kuka",Context.MODE_PRIVATE);
switch (view.getId()) {
case R.id.btnSave:
Editor editr = pres.edit();
editr.putString("name", editName.getText().toString());
editr.putInt("age", Integer.valueOf(editAge.getText().toString()));
editr.commit();
Toast.makeText(SharedPreferencesActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
break;
case R.id.btnShow:
String name =pres.getString("name", "NO");
String age = String.valueOf(pres.getInt("age", 0));
String btr =pres.getString("btr", "NO");
textResult.setText("Name: "+name+" Age: "+age+" ;btr: "+btr);
break;
}
}
};
}
main.xml配置信息如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="姓名:"
android:textStyle="bold"
android:textSize="20dp"
/>
<EditText
android:id="@+id/editName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="coosyz"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="年龄:"
android:textStyle="bold"
android:textSize="20dp"
/>
<EditText
android:id="@+id/editAge"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="23"
/>
</TableRow>
<Button
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:layout_width="fill_parent"
android:text="保存"
/>
<Button
android:layout_height="wrap_content"
android:id="@+id/btnShow"
android:layout_width="fill_parent"
android:text="显示"
/>
<TextView
android:id="@+id/textResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示区"
/>
</TableLayout>
如图:
还有一点需要注意的就是利用SharedPreferences保存信息时,有一句话比较重要相当于对数据库的操作是事务管理差不多要对信息进行提交 editr.commit();这句,如果信息保存有错误就不会执行commit方法。