PreferenceActivity简单用法

Android3.0以前采用PreferenceActivity ,3.0以后采用PreferenceFragment,之后讲解,今天只写PreferenceActivity,大伙凑合看吧,能学点学点

很简单只需要继承PreferenceActivity,然后采用addPreferencesFromResource(R.xml.preferences);加载布局即可

activity代码:

package com.example.preferenceactivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button showAll;
private Button show;
private TextView list;
private TextView edit;
private TextView checkbox;
private Button removeOne;
private Button clearAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
// TODO Auto-generated method stub
showAll = (Button) findViewById(R.id.showAll);
show = (Button) findViewById(R.id.show);
removeOne = (Button)findViewById(R.id.removeOne);
clearAll = (Button)findViewById(R.id.clearAll);
checkbox = (TextView) findViewById(R.id.checkout);
list = (TextView) findViewById(R.id.list);
edit = (TextView) findViewById(R.id.edit);
}
private void initListener() {
// TODO Auto-generated method stub
showAll.setOnClickListener(this);
show.setOnClickListener(this);
removeOne.setOnClickListener(this);
clearAll.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//展示所有
case R.id.showAll:
showAll();
break;
//删除一个
case R.id.removeOne:
removeOne();
break;
//清空所有
case R.id.clearAll:
clearAll();
break;
//展示数据
case R.id.show:
showResult();
break;
default:
break;
}
}
//显示所有配置
private void showAll() {
startSetting("showAll");
}
//删除一个配置
private void removeOne() {
// TODO Auto-generated method stub
startSetting("removeOne");
}
//清除所有配置
private void clearAll() {
// TODO Auto-generated method stub
startSetting("clearAll");
}
    public void startSetting(String text) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(getApplicationContext(), Setting.class);
intent.putExtra("choose", text);
startActivity(intent);
    }
  //获取sp中系统保存的值
  @SuppressWarnings("deprecation")
  private void showResult() {
  // TODO Auto-generated method stub
  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  boolean b = sharedPreferences.getBoolean("fuxuan", false);
  checkbox.setText(b+"");
  String  list1 = sharedPreferences.getString("danxuan", "haha");
  list.setText(list1);
  String editbox = sharedPreferences.getString("edit", "bianji");
  edit.setText(editbox);
  }
}

setting代码:

public class Setting extends PreferenceActivity {


@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
String extra = intent.getStringExtra("choose");
switch (extra) {
case "showAll":
addPreferencesFromResource(R.xml.preferences);
break;
case "removeOne":
addPreferencesFromResource(R.xml.preferences);
//删除一个ListPreference以此为例
PreferenceCategory category = (PreferenceCategory) findPreference("duoge");
category.removePreference(findPreference("danxuan"));
break;
case "clearAll":
//删除所有配置信息
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().removeAll();
break;
default:
break;
}

}
}

在res目录下创建一个xml文件夹,在文件夹下创建文件preferences.xml,里面内容如下

  <PreferenceScreen 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="screen" >  
      <PreferenceCategory 
          android:title="个人"
          android:key="geren"
          >
          <CheckBoxPreference 
              android:key="fuxuan"
              android:title="复选框"
              android:summaryOn="选择上了"
             android:summaryOff="么选择"
             android:icon="@drawable/ic_launcher"
              
              />
      </PreferenceCategory>
      <PreferenceCategory 
          android:title="公司"
          android:key="duoge"
          >
          <ListPreference 
              android:key="danxuan"
              android:title="单选框"
              android:summary="这是单选框"
             android:entries="@array/name"//这个参数和下面这个参数一定要写,不写报错
             android:entryValues="@array/name"
              />
          <EditTextPreference 
              android:key="edit"
              android:title="编辑栏"
              android:summary="随便写写啦"
              android:dialogTitle="编辑"
              android:dialogMessage="hh"
              android:dialogIcon="@drawable/ic_launcher"
              />
      </PreferenceCategory>
      
    </PreferenceScreen> 

在values下创建文件arrays.xml,里面内容如下:

<?xml version="1.0" encoding="utf-8"?>    
    <resources>    
        <string-array name="name">    
            <item>mifeilong</item>    
            <item>zhangxue</item>    
            <item>wujing</item>    
        </string-array>    
    </resources>   

主activity布局activity_main里面内容如下:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.preferenceactivity.MainActivity" >
<Button 
   android:text="showAll"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/showAll"
   />
<Button 
   android:id="@+id/removeOne"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="removeOne"/>
<Button 
   android:id="@+id/clearAll"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="clearAll"/>
<Button 
   android:text="show"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/show"
   />
    <TextView
        android:id="@+id/checkout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="checkbox" />
     <TextView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="list" />
      <TextView
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="checkbox" />
</LinearLayout>

好了大功告成,就是这么简单,赶紧上手试

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值