android应用开发揭秘之实现examples_06_01(shared preferences存储参数功能)

      本例用来测试存储参数功能,这在很多应用软件中经常用到。 是的,在windows应用程序通常会采用ini方式。而在android中,有人写好了这个SharedPreferences类并提供了一些方法

      其实几乎没有照抄examples_06_01, 完全由自己写出来。
      界面很简单:
android应用开发揭秘之实现examples_06_01(shared preferences存储参数功能) - 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="保存设置" 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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值