2

我正在使用我PreferenceActivity没有SwitchPreference使用标题的地方,android:title="Title"因为我想在Switch单击时更改它。

我已经尝试过这种方式,但没有奏效。我还搜索了与 SO 相关的主题,但没有解决我的问题。

public class PrefsFragment extends PreferenceFragment {

    private SwitchPreference pref;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
        // Load the preferences from an XML resource

        pref = (SwitchPreference)findPreference("SWITCH");
        pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (newValue.toString().equals("SWITCH")) {
                    boolean test = (Boolean) newValue;
                    if (test) {
                        pref.setTitle("ChangedTitle");
                    } else {
                        pref.setTitle("DefaultTitle");
                    }
                }

                return true;
            }
        });
    }
}

settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="Settings">

        <ListPreference
            android:key="yourPref"
            android:title="Title"
            android:dialogTitle="Select"
            android:summary="%s"
            android:defaultValue="4"
            android:entries="@array/titles"
            android:entryValues="@array/values"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="Title">

        <SwitchPreference
            android:key="SWITCH"/>

    </PreferenceCategory>
</PreferenceScreen>

请问你能帮帮我吗?

4

2 回答 2

0

尝试preference.setTitle("YOUR TEXT");在侦听器回调中调用,而不是pref.setTitle"YOUR TEXT");侦听器传入已更改的首选项。

于 2016-08-25T19:45:01.140 回答
0

问题在于您的 OnSharedPreferenceChangeListener。取而代之的是,

pref = (SwitchPreference)findPreference("SWITCH");
        pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (newValue.toString().equals("SWITCH")) {
                    boolean test = (Boolean) newValue;
                    if (test) {
                        pref.setTitle("ChangedTitle");
                    } else {
                        pref.setTitle("DefaultTitle");
                    }
                }

                return true;
            }
        });

尝试这个

SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
        if (s.equals("SWITCH")) {
            SwitchPreference sp = (SwitchPreference) findPreference(s);
            boolean test = sharedPreferences.getBoolean(s, false);
            if (test) { 
                sp.setTitle("ChangedTitle"); 
            } else { 
                sp.setTitle("DefaultTitle");
            }
        }
    }
}
    
于 2020-10-22T13:56:51.860 回答