android之首选项相关 Preferences(三)EditTextPreference/RingtonePreference

本文详细介绍了EditTextPreference和RingtonePreference的使用方法,包括它们的代码实现和存储方式。
EditTextPreference:
效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0066/6876/79e14479-3c75-3894-bb60-39078697dd63.jpg[/img]
[/img]

当我们点击主页面的输入名称时,就会弹出该对话框,让我们输入名称。
代码:
<?xml version="1.0" encoding="utf-8"?>   
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:title="屏幕标题"
android:summary="屏幕简要说明"
>

<EditTextPreference
android:dialogTitle="输入您的名称:"
android:key="editTitlePreference"
android:summary="简要说明"
android:title="输入名称"
></EditTextPreference>
</PreferenceScreen>


RingtonePreference:
效果图
[img]
[img]http://dl.iteye.com/upload/attachment/0066/6878/62b2a23f-c569-3396-b242-2005376350b0.jpg[/img]
[/img]

代码:
<?xml version="1.0" encoding="utf-8"?>   
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="edittext_screen"
android:title="屏幕标题"
android:summary="屏幕简要说明"
>
<!--
android:ringtoneType 设置响铃模式,主要包括ringtone、notification、alarm、all
android:showSilent 是否显示静音
注意,如果模拟器中没有铃声的话,我们可以自己添加。将音乐复制到SD卡上,然后转到android media player应用程序,选择该音乐,
单击menu,然后选择 uses as ringtone

-->
<RingtonePreference
android:key="ringtonePreference"
android:summary="简要说明"
android:title="选择系统铃声"
android:ringtoneType="alarm"
android:showSilent="true"
></RingtonePreference>

</PreferenceScreen>


我们看看后台的xml中是如何存储的 

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="ringtonePreference">content://settings/system/alarm_alert</string>

</map>
这里我们可要注意了哦,ringtonePreference的值是一个uri字符串
package com.brome.usbnet; /** * 设置界面活动类 * 功能:管理应用的偏好设置,支持AP配置、路由设置、DNS设置和功耗控制 * 界面类型:单窗格列表(强制简化模式,不支持平板双面板) */ import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.RingtonePreference; import android.text.TextUtils; import android.preference.SwitchPreference; // 使用标准库中的 SwitchPreference import java.util.List; import android.content.ServiceConnection; import android.util.Log; /** * A {@link PreferenceActivity} that presents a set of application settings. On * handset devices, settings are presented as a single list. On tablets, * settings are split by category, with category headers shown to the left of * the list of settings. * <p> * See <a href="http://developer.android.com/design/patterns/settings.html"> * Android Design: Settings</a> for design guidelines and the <a * href="http://developer.android.com/guide/topics/ui/settings.html">Settings * API Guide</a> for more information on developing a Settings UI. */ public class SettingsActivity extends PreferenceActivity { private CheckBoxPreference powerControlSwitch; private ListPreference powerControlOptions; private PowerControl powerControl; private usbNetService mService; // 新增:用于存储 usbNetService 实例; private static final String TAG = "SettingsActivity"; /** * Determines whether to always show the simplified settings UI, where * settings are presented in a single list. When false, settings are shown * as a master/detail two-pane view on tablets. When true, a single pane is * shown on tablets. */ private static final boolean ALWAYS_SIMPLE_PREFS = false; /////////////////////////////////////////////// @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); setupSimplePreferencesScreen(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 获取 usbNetService 实例 Intent intent = getIntent(); if (intent != null) { mService = (usbNetService) intent.getSerializableExtra("usbNetService"); // 假设通过 Intent 传递了 usbNetService 实例 } if (mService != null) { // 获取 AccessoryForwarder 实例 AccessoryForwarder forwarder = mService.getAccessoryForwarder(); // 假设 usbNetService 有 getAccessoryForwarder() 方法 // 初始化 PowerControl 类 powerControl = new PowerControl(this, forwarder); } } /** * Shows the simplified settings UI if the device configuration if the * device configuration dictates that a simplified, single-pane UI should be * shown. */ private void setupSimplePreferencesScreen() { //if (!isSimplePreferences(this)) { // return; //} Intent intent = getIntent(); int ApEnable = intent.getIntExtra("ApEnable",-1); String ApSSID = intent.getStringExtra("ApSSID"); String ApPasswd = intent.getStringExtra("ApPasswd"); //addPreferencesFromResource(R.xml.pref_sleep); if(ApEnable >= 0 && ApEnable <=1 ) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); preferences.edit() .putBoolean("pref_enable_ap", ApEnable==1?true:false) .putString("pref_ap_ssid",ApSSID) .putString("pref_ap_passwd",ApPasswd) .commit(); addPreferencesFromResource(R.xml.pref_ap); bindPreferenceSummaryToValue(findPreference("pref_ap_ssid")); bindPreferenceSummaryToValue(findPreference("pref_ap_passwd")); } addPreferencesFromResource(R.xml.pref_route); addPreferencesFromResource(R.xml.pref_dns); bindPreferenceSummaryToValue(findPreference("pref_enable_default")); bindPreferenceSummaryToValue(findPreference("pref_enable_dns")); bindPreferenceSummaryToValue(findPreference("static_route0")); bindPreferenceSummaryToValue(findPreference("static_route1")); bindPreferenceSummaryToValue(findPreference("static_route2")); bindPreferenceSummaryToValue(findPreference("static_dns0")); // 加载功耗控制布局文件的代码 addPreferencesFromResource(R.xml.pref_power_control); bindPreferenceSummaryToValue(findPreference("pref_enable_power_control")); bindPreferenceSummaryToValue(findPreference("pref_power_control_options")); ///////////////////////////////////////////////////////////////////// // 获取功耗控制开关和自动休眠时间设置 Preference powerControlSwitchPref = findPreference("pref_enable_power_control"); Preference powerControlOptionsPref = findPreference("pref_power_control_options"); // 检查偏好设置是否存在 if (powerControlSwitchPref == null || powerControlOptionsPref == null) { Log.e(TAG, "Preference not found: powerControlSwitch=" + (powerControlSwitchPref == null) + ", powerControlOptions=" + (powerControlOptionsPref == null)); return; } // 安全地转换类型 try { powerControlSwitch = (CheckBoxPreference) powerControlSwitchPref; powerControlOptions = (ListPreference) powerControlOptionsPref; } catch (ClassCastException e) { Log.e(TAG, "Error casting preferences", e); return; } //////////////////20250610/////////////////////// // 设置监听 powerControlSwitch.setOnPreferenceChangeListener(powerControlChangeListener); powerControlOptions.setOnPreferenceChangeListener(powerControlChangeListener); // 触发初始值监听 powerControlChangeListener.onPreferenceChange(powerControlSwitch, PreferenceManager.getDefaultSharedPreferences(this) .getBoolean(powerControlSwitch.getKey(), false)); powerControlChangeListener.onPreferenceChange(powerControlOptions, PreferenceManager.getDefaultSharedPreferences(this) .getString(powerControlOptions.getKey(), null)); //////////////////20250610/////////////////////// } //////////////////20250610/////////////////////// private Preference.OnPreferenceChangeListener powerControlChangeListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference.getKey().equals("pref_enable_power_control")) { boolean isEnabled = (boolean) newValue; powerControlOptions.setEnabled(isEnabled); if (isEnabled) { // 启用功耗控制,触发获取休眠时间 powerControl.getAutoSleepTime(); } } else if (preference.getKey().equals("pref_power_control_options")) { if (powerControlSwitch.isChecked()) { // 启用功耗控制且自动休眠时间改变,触发获取休眠时间 powerControl.getAutoSleepTime(); } } return true; } }; //////////////////20250610/////////////////////// /** {@inheritDoc} */ @Override public boolean onIsMultiPane() { return isXLargeTablet(this) && !isSimplePreferences(this); } /** * Helper method to determine if the device has an extra-large screen. For * example, 10" tablets are extra-large. */ private static boolean isXLargeTablet(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; } /** * Determines whether the simplified settings UI should be shown. This is * true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device * doesn't have newer APIs like {@link PreferenceFragment}, or the device * doesn't have an extra-large screen. In these cases, a single-pane * "simplified" settings UI should be shown. */ private static boolean isSimplePreferences(Context context) { return ALWAYS_SIMPLE_PREFS; //|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ; //|| !isXLargeTablet(context); } /** {@inheritDoc} */ @Override @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onBuildHeaders(List<Header> target) { if (!isSimplePreferences(this)) { //loadHeadersFromResource(R.xml.pref_headers, target); } } private void bindPreferenceSummaryToValue(Preference preference) { // Set the listener to watch for value changes. preference .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); // Trigger the listener immediately with the preference's // current value. if (preference instanceof CheckBoxPreference) { sBindPreferenceSummaryToValueListener.onPreferenceChange( preference, PreferenceManager .getDefaultSharedPreferences(preference.getContext()) .getBoolean(preference.getKey(),false) ); } else if (preference instanceof EditTextPreference) { sBindPreferenceSummaryToValueListener.onPreferenceChange( preference, PreferenceManager .getDefaultSharedPreferences(preference.getContext()) .getString(preference.getKey(),"") ); } } /** * A preference value change listener that updates the preference's summary * to reflect its new value. */ private Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { String stringValue = value.toString(); if (preference instanceof CheckBoxPreference) { if(preference.getKey().equals("pref_enable_default")) { boolean enable = (Boolean)value; findPreference("static_route0").setEnabled(!enable); findPreference("static_route1").setEnabled(!enable); findPreference("static_route2").setEnabled(!enable); } else if(preference.getKey().equals("pref_enable_dns")) { boolean enable = (Boolean)value; findPreference("static_dns0").setEnabled(enable); } }else { preference.setSummary(stringValue); } return true; } }; /** * This fragment shows general preferences only. It is used when the * activity is showing a two-pane settings UI. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class GeneralPreferenceFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.pref_route); addPreferencesFromResource(R.xml.pref_dns); addPreferencesFromResource(R.xml.pref_power_control); } } }<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/pref_title_power_control_setting" /> <CheckBoxPreference android:defaultValue="false" android:key="pref_enable_power_control" android:summary="@string/pref_description_enable_power_control" android:title="@string/pref_title_enable_power_control" /> <ListPreference android:entries="@array/pref_power_control_entries" android:entryValues="@array/pref_power_control_values" android:key="pref_power_control_options" android:title="@string/pref_title_power_control_options" /> </PreferenceScreen>当我勾选启用能耗控制的选项时候程序崩溃了帮我分析代码价检查原因
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值