Android 在“恢复”中显示单选按钮的值
我在Android 在“恢复”中显示单选按钮的值,android,android-layout,sharedpreferences,Android,Android Layout,Sharedpreferences,我在resume方法中存储了单选按钮的值,但当我尝试注册这些值时,未检测到任何值。奇怪的是,我看到选中了单选按钮,但值为零。 所以,当我进入数据库时,我有a=0和b=0,这意味着单选按钮没有被点击, 当我再次点击单选按钮,然后点击该按钮,我在数据库中有正确的值,我要寻找的是我不必再次点击单选按钮 public class ActivityUn extends Activity { public void ajouter(View v) { db.open(
resume
方法中存储了单选按钮的值,但当我尝试注册这些值时,未检测到任何值
。奇怪的是,我看到选中了单选按钮,但值为零。
所以,当我进入数据库时,我有a=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 " />
我发现您的代码中有两个错误 首先,您的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();
有关更多信息,请参见和使用
radiobuttonId()
和radiobuttonId()
和RadioButton.check()
,而不是在单个单选按钮上调用RadioButton.isChecked()
和RadioButton.setChecked()
。使用RadioGroup.setOnCheckedChangeListener()
(通过代码,而不是xml布局)来收听RadioGroup中的更改
如果有人对此有所了解,我将非常感激,我花了几天时间寻找解决方案,但没有找到,请帮助!!!发布活动\u un.xml布局。好的,我现在就发布。@GareginSargsyan:你能解释一下吗?!这个问题没有解决方案吗,我完成了我的应用程序,我只需要解决这个问题,给我一个提示,我会做剩下的事情..谢谢你的回答,是的,我已经解决了这两个问题,问题是我必须再次单击单选按钮来获取值,但我已经存储在共享首选项中,如何解决!!你的意思是当你的应用程序恢复时,没有从共享首选项中获取值?是的,我看到单选按钮已选中,但当我单击按钮以注册onRadioButtonClicked中指定的值时,我将0作为值,这意味着未单击单选按钮!!!请看我的编辑,使用RadioGroup而不是RadioButtonTanks做了很多工作,我希望它能起作用!感谢您的想法,我将尝试并告诉您结果,只是为了明确我应该在Onresume和OnPause方法中更改它,您能帮我举一个代码示例吗:)