android 自定义switchpreference,如何设置默认值为 SwitchPreference 的 android 吗

这篇博客探讨了在Android中如何为SwitchPreference设置默认值的问题。作者遇到即使设置了默认值,SwitchPreference在初次加载时仍显示错误的默认状态。解决方案是通过SharedPreferences在程序启动时设置所需的默认值,确保在添加到PreferenceCategory之前设置正确的状态。

匿名用户

1级

2015-01-18 回答

没有人用 SwitchPreference 类从 android ,知道如何去设置的默认值吗?我有以编程方式实现它:

SwitchPreference switch = new SwitchPreference(this);

switch.setKey("preference_my_key");

switch.setTitle(R.string.preference_title_my_title);

switch.setSummary(R.string.preference_summary_my_summary);

Boolean isChecked = Manager.myMethodIsChecked(MyActivity.this);

switch.setChecked( isChecked );

switch.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

@Override

public boolean onPreferenceChange(Preference preference, Object newValue) {

try {

boolean selected = Boolean.parseBoolean(newValue.toString());

if ( !selected ) {

//do something

}

} catch (Throwable e) {

e.printStackTrace();

}

return true;

}

});

category.addPreference(switch);

首选项将所有值入都保存其 xml 文件: app_package_name_preferences.xml。

第一次 time 的应用程序加载时, switch 有"虚假"的默认值。

但我有时需要使默认值"true"。

我试过几种方法,但没有什么工作。

switch.setChecked( true );

switch.setDefaultValue(true);

请帮助我,如果有人知道如何设置此默认值。感谢你在前进 Lidia

解决方法 1:

我为我的问题找到解决办法并将发布它的情况下有人有同样的问题。

正如我告诉,我以编程方式编写首选项

PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

PreferenceCategory catView = new PreferenceCategory(this);

catView.setTitle(R.string.preference_category_view);

root.addPreference(catView);

final SwitchPreference switchSplash= new SwitchPreference(this);

switchSplash.setKey(PreferenceKeys.SPLASH);

//-----the above code----

switchSplash.setChecked(false); // LINE 1

catView.addPreference(switchSplash); // LINE 2

在 debug 我发现真正的价值设置在行 1,但当我将switchSplash添加到catView, switchSplash的值重置为false,因为catView设置的值从 prefereces xml 。

这就是为什么我改变成所需的所有值xml

SharedPreferences.Editor editor = root.getPreferenceManager().getSharedPreferences().edit();

editor.putBoolean(PreferenceKeys.SPLASH, true);

editor.commit();

这是它。

好的,以下是一个简单的实现步骤: 首先,在xml布局文件中定义PreferenceScreen和SwitchPreference,如下所示: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <SwitchPreference android:key="switch_preference" android:title="Switch Preference" android:summary="This is a switch preference" android:widgetLayout="@layout/switch_preference_layout"/> </PreferenceScreen> ``` 其中,`android:widgetLayout`属性用于指定SwitchPreference使用自定义布局,值为自定义布局的资源ID。 接下来,创建自定义布局文件`switch_preference_layout.xml`,并添加Switch、Title、Icon,如下所示: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <Switch android:id="@+id/switch_widget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@id/switch_widget" android:text="Switch Preference" android:textSize="16sp" /> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toStartOf="@id/switch_widget" android:src="@drawable/ic_launcher" /> </RelativeLayout> ``` 在自定义布局文件中,使用RelativeLayout布局,分别添加了Switch、Title、Icon三个控件,并设置了相应的属性。 最后,在Activity中读取SwitchPreference的状态,可以通过以下代码实现: ```java SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); boolean switchPreferenceValue = preferences.getBoolean("switch_preference", false); ``` 其中,`"switch_preference"`为SwitchPreference的key属性,`false`为默认值。 以上就是一个简单的实现步骤,你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值