2013-09-21 56 views
0

我有兩個活動(設置,祈禱)如何從一個RadioGroup中sharedpreferences到anotrher activitys

在設置活動我把無線電集團內部的三個單選按鈕

每個單選按鈕將讓顏色在祈禱改變活動

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); 
} 
} 

並在祈禱活性的文本顏色和佈局應該改變顏色,以在設置活動

0123被選擇的選擇中的一個的背景個

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

你正在編寫,並從兩個不同的SharedPreferences閱讀:

在SettingsActivity:

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

在祈禱:

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

通知的第一個參數即SharedPreference文件的名稱。

+0

我改變了第二個,我像第一個寫ite,但它不起作用的顏色不會改變 – BasILyoS

+0

呈現的活動順序是什麼?重新啓動應用程序是否反映了先前調用中所做的更改? – Rajesh

相關問題