onPreferenceClick不适用于我

时间:2014-05-24 10:19:30

标签: android onpreferenceclicklistener

我尝试在 onPreferenceClick 中执行一些代码,但它没有用。

我制作了一个SettingActivity Calss,并扩展了PreferenceActivity。

然后我实现了OnPreferenceClickListener。

这是我的设置活动类

    DecimalFormat df = new DecimalFormat("@##,###,###,###");
private Calculation mCalculation;
private TextView mCalculatorDisplay;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

}

@SuppressWarnings("deprecation")
@Override
public boolean onPreferenceClick(Preference preference) {
    if (preference.getKey().equals("Point")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(",", "."));

    } else if (preference.getKey().equals("Comma")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(".", ","));

    } else if (preference.getKey().equals("Space")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(",", " "));

    } else if (preference.getKey().equals("Off")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult()).replaceAll(" ", ""));


        getPreferenceManager().getSharedPreferences().edit()
        .putInt("unreadcount", 0).commit();
    }
    return true;
}

这是我的arrays.xml

    <string-array name="decimalNumber">
    <item name="P">Point</item>
    <item name="C">Comma</item>
    <item name="S">Space</item>
    <item name="O">Off</item>
</string-array>
<string-array name="decimalNumberValues">
    <item name="P">P</item>
    <item name="C">C</item>
    <item name="S">S</item>
    <item name="O">O</item>
</string-array>

xml文件夹中的settings.xml:

    <PreferenceCategory android:title="@string/pref_user_settings" >
    <ListPreference
        android:entries="@array/decimalNumber"
        android:entryValues="@array/decimalNumberValues"
        android:key="prefdecimalNumber"
        android:summary="@string/number_format_summary"
        android:title="@string/number_format" />
</PreferenceCategory>

这是我的用户设置的照片: enter image description here

我想要做的是点击每一个改变十进制数格式的方式。

提前致谢:)

更新:我这样做是为了注册PreferenceClickListener。

    @SuppressWarnings("deprecation")
@Override
protected void onPause() {
    super.onPause();

    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(
                    (OnSharedPreferenceChangeListener) this);
}

@SuppressWarnings("deprecation")
@Override
protected void onResume() {
    super.onResume();

    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(
                    (OnSharedPreferenceChangeListener) this);
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    if (key.equals("Point")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult())
                .replaceAll(",", "."));
    } else if (key.equals("Comma")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult())
                .replaceAll(".", ","));
    } else if (key.equals("Space")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult())
                .replaceAll(",", " "));
    } else if (key.equals("Off")) {
        mCalculatorDisplay.setText(df.format(mCalculation.getResult())
                .replaceAll(" ", ""));
    }
}

它给了我这个错误:

05-24 11:10:24.127:E / AndroidRuntime(1025):java.lang.RuntimeException:无法恢复活动{com.com.ecalc / com.com.ecalc.Z_SettingsActivity}:java.lang.ClassCastException :com.com.ecalc.Z_SettingsActivity无法强制转换为android.content.SharedPreferences $ OnSharedPreferenceChangeListener

1 个答案:

答案 0 :(得分:0)

我认为你必须在onPause()和onStart()中的sharedPreferenceChangeListener上注册你的对象 - methode

看这里:https://stackoverflow.com/a/16449611/2924941