单选按钮状态不保存

时间:2014-08-30 05:28:44

标签: android sharedpreferences radio-group

这是我用于显示绘制模拟时钟的颜色的代码,为此我使用了rado组并通过我的动态壁纸中的共享prefence使用它,我点击的颜色成功应用于时钟,但问题是关闭后再次返回到采色窗口时,前一个单选按钮状态未保存,任何人都可以帮我纠正错误

protected void onDialogClosed(boolean flag) {
    flag1=flag;
    super.onDialogClosed(flag1);
    if (flag1) {
        android.content.SharedPreferences.Editor editor = getEditor();
        saveRadioButton(editor, 0x7f080001, 0xffffff);
        saveRadioButton(editor, 0x7f080002, 0xc0c0c0);
        saveRadioButton(editor, 0x7f080003, 0x808080);
        saveRadioButton(editor, 0x7f080004, 0xff0000);
        saveRadioButton(editor, 0x7f080005, 0xff8000);
        saveRadioButton(editor, 0x7f080006, 0xffff00);
        saveRadioButton(editor, 0x7f080007, 0x80ff00);
        saveRadioButton(editor, 0x7f080008, 65280);
        saveRadioButton(editor, 0x7f080009, 65408);
        saveRadioButton(editor, 0x7f08000a, 65535);
        saveRadioButton(editor, 0x7f08000b, 33023);
        saveRadioButton(editor, 0x7f08000c, 255);
        saveRadioButton(editor, 0x7f08000d, 0x8000ff);
        saveRadioButton(editor, 0x7f08000e, 0xff00ff);
        saveRadioButton(editor, 0x7f08000f, 0xff0080);
        editor.commit();
    }
}

protected void saveRadioButton(android.content.SharedPreferences.Editor editor, int i, int j) {

    if (((RadioButton)mainView.findViewById(i)).isChecked()) {
        editor.putInt(getKey(), j);
    }

}

2 个答案:

答案 0 :(得分:0)

无需编写如此多的方法调用,view.getId()将返回特定的id,并且您只在SharedPreferences中保存值。你应该在适当的地方从SharedPreferences获得价值。

答案 1 :(得分:0)

尝试在

之后调用editor.commit();
editor.putInt(getKey(), j);
editor.commit();