PreferenceFragment加载设置页面

来源:互联网 发布:淘宝远望数码 编辑:程序博客网 时间:2024/04/28 17:39

新的SDK中已经推荐PreferenceFragment代替PreferenceActivity展示设置页面,主要设置参数如下:

XML文件:

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2008 The Android Open Source Project     Licensed under the Apache License, Version 2.0 (the "License");     you may not use this file except in compliance with the License.     You may obtain a copy of the License at          http://www.apache.org/licenses/LICENSE-2.0     Unless required by applicable law or agreed to in writing, software     distributed under the License is distributed on an "AS IS" BASIS,     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     See the License for the specific language governing permissions and     limitations under the License.--><PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">    <PreferenceCategory        android:title="@string/wifi_tether_setting_title"        android:persistent="false"/>    <ListPreference        android:key="wifi_auto_disable"        android:title="@string/wifi_tether_auto_disable_title"        android:persistent="false"        android:entries="@array/wifi_hotspot_disable_policy_entries"        android:entryValues="@array/wifi_hotspot_disable_policy_values"/>    <Preference        android:key="wifi_ap_ssid_and_security"        android:title="@string/wifi_tether_configure_ap_text"        android:persistent="false" />    <CheckBoxPreference            android:key="suspend_optimizations"            android:title="@string/wifi_hotspot_suspend_optimizations"            android:summary="@string/wifi_hotspot_suspend_optimizations_summary"            android:persistent="false" />    <Preference        android:key="wps_connect"        android:title="@string/wifi_tether_wps_connect_title"        android:summary="@string/wifi_tether_wps_connect_summary"         android:persistent="false" />    <Preference        android:key="bandwidth_usage"        android:title="@string/wifi_ap_bandwidth_title"        android:fragment="com.mediatek.wifi.hotspot.BandwidthUsage"        android:persistent="false" />    <PreferenceCategory        android:key="connected_category"        android:title="@string/wifi_ap_connected_title"        android:persistent="false"/>    <PreferenceCategory        android:key="blocked_category"        android:title="@string/wifi_ap_blocked_title"        android:persistent="false"/></PreferenceScreen>

Activity为:

package com.example.wifihotspot2;import android.preference.PreferenceActivity;import android.app.Activity;import android.content.SharedPreferences;  import android.os.Bundle;  import android.preference.EditTextPreference;  import android.preference.ListPreference;  import android.preference.Preference;  import android.preference.Preference.OnPreferenceChangeListener;  import android.preference.PreferenceActivity;  import android.preference.PreferenceManager;  import android.text.AndroidCharacter;import android.view.View;  import android.preference.PreferenceFragment;  public class SettingsPreferenctScreenActivity extends Activity { SharedPreferences prefs;  /* (non-Javadoc) * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();}  public static class PrefsFragement extends PreferenceFragment{/* (non-Javadoc) * @see android.preference.PreferenceFragment#onCreate(android.os.Bundle) */@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);addPreferencesFromResource(R.xml.tether_wifi_prefs);}      }}



0 0
原创粉丝点击