单击时如何更改SwitchPreference标题

时间:2016-08-25 18:39:48

标签: android android-preferences preferenceactivity switchpreference

我正在使用我的PreferenceActivitySwitchPreference我没有使用android:title="Title"来确定标题,因为我想在{Switch时更改它单击1}}。

我尝试过这种方式但没有奏效。我也在搜索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>

你能帮我吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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");
            }
        }
    }
}