我看了,並嘗試不同的方法,所以我絕對做錯了什麼。從另一個活動的sharedpreferences中檢索radiogroup選擇?
我在「磅」或「千克」的主要活動中有一個radiogroup選擇。無論他們選擇哪個收音機,都應在下一個活動中更改爲該文本。
Lbs是當前的默認無線電,並被選中爲「true」。
我已經在第二個活動中放置了一個按鈕返回到收音機選擇屏幕。但是,選擇「kgs」後,它會繼續顯示「lbs」。
主要活動
@Override
public void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
String enteredWeightState = sharedPreferences.getString("entered_weight", "");
int lbsState = sharedPreferences.getInt("UOM_checkId", UOM_RadioGroup.getCheckedRadioButtonId());
UOM_RadioGroup.check(lbsState);
enter_weight_editText.setText(enteredWeightState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UOM_RadioGroup = (RadioGroup) findViewById(R.id.UOM_RadioGroup);
enter_weight_editText = (EditText)findViewById(R.id.enter_weight_editText);
UOM_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup UOM_RadioGroup, int checkedId) {
if (checkedId == R.id.UOM_pounds) {
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("UOM_checkId", checkedId);
editor.apply();
} else {
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("UOM_checkId", checkedId);
editor.apply();
}
}
});
}
public void beginRoutineButton(View view) {
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("entered_weight", enter_weight_editText.getText().toString());
editor.apply();
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
Activity2
public TextView unit_of_measure;
public View fragment1;
public Button backButton;
public TextView weight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
weight = (TextView) findViewById(R.id.weight);
backButton = (Button) findViewById(R.id.backButton);
unit_of_measure = (TextView) findViewById(R.id.unit_of_measure);
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
int lbsState = sharedPreferences.getInt("UOM_checkId", -1);
String enteredWeightState = sharedPreferences.getString("entered_weight", "");
weight.setText(enteredWeightState);
if (lbsState < 0)
unit_of_measure.setText("kgs");
else
unit_of_measure.setText("lbs");
}
public void backButton(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}