在主要活动我有单选按钮,在方法public void onRadioButtonClicked(查看视图)我将选中的每个单选按钮与一个值相关联,所以我有两个变量a和b,之后我点击按钮bton i根据方法ajouter,a和b值进入database.Until现在一切都是okey,我决定使用OnResume和OnPause方法来保存活动状态,所以当我返回活动时我看到单选按钮被选中,问题来自我点击按钮保存信息,我得到0作为数据库中的a和b的值,而不是我看到选择的值,而奇怪的是,当我再次点击单选按钮,之后保存按钮,我得到正确的价值!我的问题是如何在不单击所选单选按钮的情况下创建a和b的值?
public class ActivityUn extends Activity {
public void ajouter(View v) {
db.open();
db.insertMENAGE(a,b);
db.close();
Toast.makeText(getApplicationContext(), "Données Enregistrées",Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_un);
Button bton = (Button)findViewById(R.id.ajoutUn);
bton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ajouter(v); }
});
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.rm_13_1:
if (checked)
a=1;
break;
case R.id.rm_13_2:
if (checked)
a=2;
break;
case R.id.rm_14_1:
if (checked)
b=1;
break;
case R.id.rm_14_2:
if (checked)
b=2;
break;
case R.id.rm_14_3:
if (checked)
b=3;
findViewById
break;
case R.id.rm_14_4:
if (checked)
b=4;
break;
}
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
SharedPreferences.Editor editor = prefs3.edit();
editor.putBoolean("questionA", rm_13_1.isChecked());
editor.putBoolean("questionB", rm_13_2.isChecked());
editor.putBoolean("questionC", rm_14_1.isChecked());
editor.putBoolean("questionD", rm_14_2.isChecked());
editor.putBoolean("questionE", rm_14_3.isChecked());
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_2);
Boolean rm_13_1A = false;
Boolean rm_13_2A = false;
Boolean rm_14_1A = false;
Boolean rm_14_2A = false;
Boolean rm_14_3A = false;
rm_13_1A = prefs3.getBoolean("questionA", false);
rm_13_2A = prefs3.getBoolean("questionB", false);
rm_14_1A = prefs3.getBoolean("questionC", false);
rm_14_2A = prefs3.getBoolean("questionD", false);
rm_14_3A = prefs3.getBoolean("questionE", false);
rm_13_1.setChecked(rm_13_1A);
rm_13_2.setChecked(rm_13_2A);
rm_14_1.setChecked(rm_14_1A);
rm_14_2.setChecked(rm_14_2A);
rm_14_3.setChecked(rm_14_3A);
}
答案 0 :(得分:0)
单选按钮应仅存在于单选按钮组中。单选按钮的特征是一次只能选择组中的一个。如果用户选择C,则取消选择所有其他单选按钮(考虑多项选择问题。您只能选择一个答案)如果您希望选择多个选项,请查看android的CheckBox view