安卓首选项preference的用法

              在安卓程序开发中,又是需要保存用户的一些习惯性的行为时,可以使用首选项的方式来解决,而不需要使用复杂的数据库。

    首先在res文件下新建一个xml文件夹,在xml文件夹下新建一个文件,取名为preference.xml。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >


    <EditTextPreference 
        android:title="Edit Text"
        android:key="name"
        android:summary="Enter your name"
        />
      <CheckBoxPreference 
          android:title="Check Box"
          android:defaultValue="true"
          android:key="checkbox"
          android:summary="check this box"
          />
      <ListPreference 
          android:title="List"
          android:key="list"
          android:summary="This is a list to choose from"
          android:entries="@array/list"
          android:entryValues="@array/lvalue"
          />
</PreferenceScreen>

其中,ListPreference这个空间用到了array字符串数组,需要新建一个。在values文件夹下,新建一个array.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="list">
        <item>option 1</item>
        <item>option 2</item>    
        <item>option 3</item>
        <item>option 4</item>    
    </string-array>
    <string-array name="lvalue">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>
</resources>

接下来,编写activity_main.xml文件,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
   <TextView 
       android:id="@+id/textview1"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:text="textview"
       />
    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="首选项"
        />
</LinearLayout>

然后,在主活动MainActivity里面,代码如下:

public Button button1;
public TextView textview1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences getPrefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean information=getPrefs.getBoolean("checkbox", false);
        if(information==true){
              Toast.makeText(getApplicationContext(),"checkbox打开", Toast.LENGTH_SHORT).show();
        }else
         Toast.makeText(getApplicationContext(), "checkbox关闭", Toast.LENGTH_SHORT).show();
        SharedPreferences getData=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String et=getData.getString("name", "Mr.Yan");
        //找不到name(指name值不存在,或者未定义)这个key值时,返回Mr.Yan,而不是为空时返回Mr.Yan
        String values=getData.getString("list", "4");

//未找到list则返回4
        if(values.contentEquals("1")){
        textview1=(TextView)findViewById(R.id.textview1);
        textview1.setText(et);//如果选中的第一个选项,则把用户先前在EditTextPreference里面输入的信息在textview便签控件上面显示出来
        }
        button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this, Prefs.class);
startActivity(intent);
}
});
    }
       
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}

新建一个新的活动Prefs,布局文件使用preference,即先前建的首选项做为布局,代码如下:

public class Prefs extends PreferenceActivity{//首选项的Activity的向上继承


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);//首选项的布局文件选项
}
   需要在AndroidManifest.xml文件中注册一下新建立的活动Prefs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值