这是个更全的例子,架构很简单,便于认真学习程序参数的存储方法。 主要就是用shared preferences 和 file
//------------------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="使用 shared pref 保存设置" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="我是会员" android:id="@+id/checkBox2"></CheckBox>
<Button android:text="使用 file 保存设置" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
//--------------- .java -----------------------
package com.stephenzhu.Examples_sharedPref;
//import android.R;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import android.app.Activity;
import android.content.Context;
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, mycheckbox2;
Button mybutton1, mybutton2;
private boolean bIfCheckUpdate = false;
private boolean bIfMember = 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);
mycheckbox2 = (CheckBox)findViewById(R.id.checkBox2);
mybutton2 = (Button)findViewById(R.id.button2);
//get shared preferences and display
SharedPreferences mypref = getPreferences(Activity.MODE_PRIVATE);
bIfCheckUpdate = mypref.getBoolean("IfCheckUpdate", false);
mycheckbox1.setChecked(bIfCheckUpdate);
//get pref from file and display
loadfilepref();
mycheckbox2.setChecked(bIfMember);
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));
}
});
mybutton2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bIfMember = mycheckbox2.isChecked();
savefilepref();
DisplayToast("write IfMember: "+Boolean.toString(bIfMember));
}
});
}
//load preferences from a file
void loadfilepref()
{
Properties mypro = new Properties();
try{
FileInputStream instream = this.openFileInput("myconfig.cfg");
mypro.load(instream);
}
catch(FileNotFoundException e)
{
return;
}
catch(IOException e)
{
return;
}
bIfMember = Boolean.valueOf(mypro.get("IfMember").toString());
}
//load preferences from a file
boolean savefilepref()
{
Properties mypro = new Properties();
mypro.put("IfMember", String.valueOf(bIfMember));
try
{
FileOutputStream outstream = this.openFileOutput("myconfig.cfg", Context.MODE_WORLD_WRITEABLE);
mypro.store(outstream, "");
}
catch(FileNotFoundException e)
{
return false;
}
catch(IOException e)
{
return false;
}
return true;
}
/* 显示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="使用 shared pref 保存设置" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="我是会员" android:id="@+id/checkBox2"></CheckBox>
<Button android:text="使用 file 保存设置" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
//--------------- .java -----------------------
package com.stephenzhu.Examples_sharedPref;
//import android.R;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import android.app.Activity;
import android.content.Context;
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, mycheckbox2;
Button mybutton1, mybutton2;
private boolean bIfCheckUpdate = false;
private boolean bIfMember = 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);
mycheckbox2 = (CheckBox)findViewById(R.id.checkBox2);
mybutton2 = (Button)findViewById(R.id.button2);
//get shared preferences and display
SharedPreferences mypref = getPreferences(Activity.MODE_PRIVATE);
bIfCheckUpdate = mypref.getBoolean("IfCheckUpdate", false);
mycheckbox1.setChecked(bIfCheckUpdate);
//get pref from file and display
loadfilepref();
mycheckbox2.setChecked(bIfMember);
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));
}
});
mybutton2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bIfMember = mycheckbox2.isChecked();
savefilepref();
DisplayToast("write IfMember: "+Boolean.toString(bIfMember));
}
});
}
//load preferences from a file
void loadfilepref()
{
Properties mypro = new Properties();
try{
FileInputStream instream = this.openFileInput("myconfig.cfg");
mypro.load(instream);
}
catch(FileNotFoundException e)
{
return;
}
catch(IOException e)
{
return;
}
bIfMember = Boolean.valueOf(mypro.get("IfMember").toString());
}
//load preferences from a file
boolean savefilepref()
{
Properties mypro = new Properties();
mypro.put("IfMember", String.valueOf(bIfMember));
try
{
FileOutputStream outstream = this.openFileOutput("myconfig.cfg", Context.MODE_WORLD_WRITEABLE);
mypro.store(outstream, "");
}
catch(FileNotFoundException e)
{
return false;
}
catch(IOException e)
{
return false;
}
return true;
}
/* 显示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();
}
}
这个例子已经打包上传到我个人邮箱,有要的留言。