【问题标题】:How can get value to PreferenceFragment如何获得 PreferenceFragment 的价值
【发布时间】:2014-11-05 09:46:23
【问题描述】:

我有这个代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference android:key="notification_settings"
        android:text="@string/impostazione"
        android:id="@+id/save_check"
        android:summary="@string/notification_title"
        android:defaultValue="false"

        ></CheckBoxPreference>

</PreferenceScreen>

现在我需要在 FragmentA 中获取 Checkbox 值:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

现在呢?

【问题讨论】:

  • 试试这个方法:sharedPreferences.getBoolean("notification_settings",false)
  • 我有defaultValue,为什么我又设置为false?
  • getBoolean() 是必需的两个参数一个是键另一个是默认值。
  • 谢谢!!!我不能投票给你评论,在“答案”中重写这个并我标记:D

标签: java android android-studio sharedpreferences


【解决方案1】:
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean isChecked = sharedPreferences.getBoolean(getString(R.string.notification_settings), false);

【讨论】:

    【解决方案2】:

    试试这个方法,希望能帮助你解决问题。

    sharedPreferences.getBoolean("notification_settings",false)
    

    getBoolean() : 需要两个参数一个是key,另一个是默认值

    【讨论】:

      【解决方案3】:
      strings.xml
      <string name="notification_setting">notification_settings</string>
      ---------------------------------
      xxx.xml
      .....
      <CheckBoxPreference android:key="@string/notification_setting"
      ....
      --------------------------------
      .java
      sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
      boolean wifiState = sharedPreferences.getBoolean(R.string.setting_wifi_key, false);
      
      wifiState is you value.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        • 2018-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 2020-11-03
        相关资源
        最近更新 更多