我有resume
方法中存储的单选按钮的值,但是当我尝试注册值时,我有NO VALUE IS DETECTED
。奇怪的是,我看到选中了单选按钮,但值为零。
所以,当我去数据库时,我有一个= 0和b = 0,这意味着没有点击单选按钮,
当我再次单击单选按钮,然后按下按钮,我在数据库中有正确的值,我正在寻找的是我不必再单击单选按钮。
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_3);
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);
}
这里是xml:
<RadioGroup
android:id="@+id/rm_13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:layout_marginTop="12dp"
android:background ="#FFF8DC"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rm_13_1"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:onClick="onRadioButtonClicked"
android:text="OUI" />
<RadioButton
android:id="@+id/rm_13_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.81"
android:onClick="onRadioButtonClicked"
android:text="NON" />
</RadioGroup>
<TextView
android:id="@+id/qm_14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
style = "@style/question"
android:text="14. Statut de l’enquêté (e) :"
/>
<RadioGroup
android:id="@+id/rm_14"
android:layout_width="838dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="0.00"
android:background="#FFF8DC"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rm_14_1"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:onClick="onRadioButtonClicked"
android:text="Chef de ménage " />
<RadioButton
android:id="@+id/rm_14_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="Epoux (se) " />
<RadioButton
android:id="@+id/rm_14_3"
android:layout_width="wrap_content"
android:onClick="onRadioButtonClicked"
android:layout_height="wrap_content"
android:text="Fils ou fille aîné (e) " />
<RadioButton
android:id="@+id/rm_14_4"
android:layout_width="wrap_content"
android:onClick="onRadioButtonClicked"
android:layout_height="wrap_content"
android:layout_weight="0.29"
android:text="Autre " />
答案 0 :(得分:1)
我在你的代码中看到两个错误。
首先你的XML中有rm_14_4
,但我从未在你的代码中看到过初始化。
第二个波纹管代码指的是一个按钮,请修复:
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_2);
你使用了RadioGroup,所以使用:
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
有关详细信息,请参阅this和
答案 1 :(得分:1)
使用RadioGroup.getCheckedRadioButtonId()
和RadioGroup.check()
而不是在单个单选按钮上调用RadioButton.isChecked()
和RadioButton.setChecked()
。使用RadioGroup.setOnCheckedChangeListener()
(通过代码,而不是xml布局)来收听RadioGroup
中的更改。