我有两项活动(设置,祈祷)
在设置活动中,我在收音机组中放置了三个单选按钮,每个单选按钮都会让祈祷活动中的颜色发生变化
settings.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);
}
}
并且在祈祷活动中,文本颜色和布局背景应该将颜色更改为设置活动中选定的选项之一
prayers.class
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);
}
我的错在哪里 任何帮助???
答案 0 :(得分:0)
您正在编写和阅读两个不同的SharedPreferences:
在SettingsActivity中:
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
祈祷:
SharedPreferences sharedPreferences = getSharedPreferences(
"com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
注意第一个参数,即SharedPreference文件的名称。