Perferenceactivity系统组建可以实现系统设置的窗体

Perferenceactivity系统组建可以实现系统设置的窗体,同时省去了我们自己去写配置文件

这里面类型很多 下面上代码 自己看效果 自己看下代码就可以懂 很简单

首先在res中创建一个文件夹xml,里面建立一个文件setting.xml代表perferenceactivity的关联文件

setting.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <CheckBoxPreference
 5         android:key="setting_key"
 6         android:summary="我是默认的描述"
 7         android:summaryOff="我没有被勾选"
 8         android:summaryOn="我被勾选了"
 9         android:title="我是标题" />
10 
11     <PreferenceCategory android:title="我是小标题" >
12         <CheckBoxPreference
13             android:key="download_pic"
14             android:summary="设置2g/3g的网络请求"
15             android:summaryOff="2g/3g禁止下载图片"
16             android:summaryOn="2g/3g下载图片"
17             android:title="手机网络下载设置" />
18     </PreferenceCategory>
19 
20     <EditTextPreference
21         android:dialogTitle="dialogtitle"
22         android:key="dialog_key"
23         android:negativeButtonText="取消"
24         android:positiveButtonText="确定"
25         android:summary="对话框summary"
26         android:title="dialog_title" />
27 
28     <ListPreference
29         android:dialogTitle="选择"
30         android:entries="@array/name"
31         android:entryValues="@array/value"
32         android:key="list_key"
33         android:positiveButtonText="确定"
34         android:summary="概要"
35         android:title="标题" />
36 
37 </PreferenceScreen>

当是listPreferenceScreen时引入一个在values文件夹下的string.xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="hello">Hello World, DemoActivity!</string>
 5     <string name="app_name">PreferenceActivity</string>
 6     <string-array name="name">
 7         <item >name1</item>
 8         <item >name2</item>
 9         <item >name3</item>
10         <item >name4</item>
11         <item >name5</item>
12     </string-array>
13     <string-array name="value">
14         <item >value1</item>
15         <item >value2</item>
16         <item >value3</item>
17         <item >value4</item>
18         <item >value5</item>
19     </string-array>
20 </resources>

 

下面这个是主界面加载的xml 很简单 就是做跳转界面使用的没其他多余作用

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/hello" />
11 
12     <Button
13         android:id="@+id/button1"
14         android:onClick="click"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="打开设置界面" />
18 
19 </LinearLayout>

 

DemoActivity.java

 1 package cn.itcast.preferencedemo;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.content.SharedPreferences;
 6 import android.os.Bundle;
 7 import android.preference.PreferenceManager;
 8 import android.view.View;
 9 import android.widget.Toast;
10 
11 public class DemoActivity extends Activity {
12     /** Called when the activity is first created. */
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main);
17         
18        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
19        String value = sp.getString("list_key", "");
20        Toast.makeText(this, value, 0).show();
21         
22     }
23     
24     public void click(View view){
25         Intent intent = new Intent(this,SettingActivity.class);
26         startActivity(intent);
27     }
28 }

 

SettingActivity.java

 1 package cn.itcast.preferencedemo;
 2 
 3 import android.os.Bundle;
 4 import android.preference.Preference;
 5 import android.preference.PreferenceActivity;
 6 import android.preference.PreferenceScreen;
 7 
 8 public class SettingActivity extends PreferenceActivity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         
13         super.onCreate(savedInstanceState);
14         addPreferencesFromResource(R.xml.setting);
15         
16     }
17     
18 }

 如果要拿里面的数据 就通过getPerferenceManager.getDefuletPerference去获取

 

posted @ 2013-01-26 23:28  王世桢  阅读(492)  评论(0编辑  收藏  举报