android应用开发揭秘之实现file与shared preferences这2种存储参数功能)

这是个更全的例子,架构很简单,便于认真学习程序参数的存储方法。 主要就是用shared preferences 和 file

android应用开发揭秘之实现file与shared preferences这2种存储参数功能) - huasoft - 快乐的机器猫 小桥加加网易分站
 
//------------------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();
    }
}

这个例子已经打包上传到我个人邮箱,有要的留言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值