本例用来测试存储参数功能,这在很多应用软件中经常用到。 是的,在windows应用程序通常会采用ini方式。而在android中,有人写好了这个SharedPreferences类并提供了一些方法
其实几乎没有照抄examples_06_01, 完全由自己写出来。
界面很简单:
//-------------------main.xml----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox android:text="是否检查更新" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<Button android:text="保存设置" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
//--------------------------------------------------------------
package com.stephenzhu.Examples_sharedPref;
//import android.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MySharedPref extends Activity {
CheckBox mycheckbox1;
Button mybutton1;
private boolean bIfCheckUpdate=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mycheckbox1 = (CheckBox)findViewById(R.id.checkBox1);
mybutton1 = (Button)findViewById(R.id.button1);
//get shared preferences and display 读取参数值
SharedPreferences mypref = getPreferences(Activity.MODE_PRIVATE);
bIfCheckUpdate = mypref.getBoolean("IfCheckUpdate", false); //得到参数值,如果获取不到就默认是false
mycheckbox1.setChecked(bIfCheckUpdate);
mybutton1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bIfCheckUpdate = mycheckbox1.isChecked();
//写入参数值
SharedPreferences mytmppref=getPreferences(0);
SharedPreferences.Editor myshareeditor = mytmppref.edit();
myshareeditor.putBoolean("IfCheckUpdate", bIfCheckUpdate);
myshareeditor.commit();
DisplayToast("write IfCheckUpdate: "+Boolean.toString(bIfCheckUpdate));
}
});
}
/* 显示Toast */
public void DisplayToast(String str)
{
Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
//设置toast显示的位置
toast.setGravity(Gravity.TOP, 0, 220);
//显示该Toast
toast.show();
}
}
界面很简单:

//-------------------main.xml----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox android:text="是否检查更新" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<Button android:text="保存设置" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
//--------------------------------------------------------------
package com.stephenzhu.Examples_sharedPref;
//import android.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MySharedPref extends Activity {
CheckBox mycheckbox1;
Button mybutton1;
private boolean bIfCheckUpdate=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mycheckbox1 = (CheckBox)findViewById(R.id.checkBox1);
mybutton1 = (Button)findViewById(R.id.button1);
//get shared preferences and display 读取参数值
SharedPreferences mypref = getPreferences(Activity.MODE_PRIVATE);
bIfCheckUpdate = mypref.getBoolean("IfCheckUpdate", false); //得到参数值,如果获取不到就默认是false
mycheckbox1.setChecked(bIfCheckUpdate);
mybutton1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bIfCheckUpdate = mycheckbox1.isChecked();
//写入参数值
SharedPreferences mytmppref=getPreferences(0);
SharedPreferences.Editor myshareeditor = mytmppref.edit();
myshareeditor.putBoolean("IfCheckUpdate", bIfCheckUpdate);
myshareeditor.commit();
DisplayToast("write IfCheckUpdate: "+Boolean.toString(bIfCheckUpdate));
}
});
}
/* 显示Toast */
public void DisplayToast(String str)
{
Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
//设置toast显示的位置
toast.setGravity(Gravity.TOP, 0, 220);
//显示该Toast
toast.show();
}
}