0

我有两个活动(设置,祈祷)

在设置活动中,我在一个单选组中放置了三个单选按钮,每个单选按钮都会让祈祷活动中的颜色发生变化

设置.class

public class SettingsActivity extends Activity {

RadioGroup rg;
TextView textCheckedID, textCheckedIndex;
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    rg = (RadioGroup) findViewById(R.id.radios);
    rg.setOnCheckedChangeListener(rgOnCheckedChangeListener);
    textCheckedID = (TextView) findViewById(R.id.checkedid);
    textCheckedIndex = (TextView) findViewById (R.id.checkedindex);
    LoadPreferences();
}
OnCheckedChangeListener rgOnCheckedChangeListener = new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    RadioButton checkedRadioButton = (RadioButton)rg.findViewById(checkedId);
    int checkedIndex = rg.indexOfChild(checkedRadioButton);
    textCheckedID.setText("checkedID = " + checkedId);
    textCheckedIndex.setText("checkedIndex = " + checkedIndex);
    SavePreferences(KEY_SAVED_RADIO_BUTTON_INDEX, checkedIndex);

}
};
private void SavePreferences(String key, int value) {
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
RadioButton savedCheckedRadioButton = (RadioButton) rg.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
}
}

并且在祈祷活动中,布局的文本颜色和背景应将颜色更改为设置活动中的选定选项之一

祈祷课

SharedPreferences sharedPreferences = getSharedPreferences(
            "com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
    int colors = sharedPreferences
            .getInt("KEY_SAVED_RADIO_BUTTON_INDEX", 0);
    if (colors == 0) {
        textview.setTextColor(getResources().getColor(R.color.Vanilla));
        linear.setBackgroundColor(getResources().getColor(R.color.Maroon));
        textview.setShadowLayer(0, 0, 0,
                (getResources().getColor(R.color.Maroon)));
    } else if (colors == 1) {
        textview.setTextColor(Color.BLACK);
        linear.setBackgroundColor(Color.WHITE);
        textview.setShadowLayer(0, 0, 0, Color.BLACK);
    } else if (colors == 2) {
        textview.setTextColor(Color.WHITE);
        linear.setBackgroundColor(Color.BLACK);
        textview.setShadowLayer((float) 1.5, 2, 2, Color.WHITE);
    }

我的错在哪里有什么帮助???

4

1 回答 1

0

您正在从两个不同的 SharedPreferences 中写入和读取:

在设置活动中:

SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);

在祈祷中:

SharedPreferences sharedPreferences = getSharedPreferences(
        "com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);

注意第一个参数,即 SharedPreference 文件的名称。

于 2013-09-21T13:36:50.417 回答