Java 如何使用收音机组的共享首选项
我在“下一步”和“上一步”中有两个按钮,问题文本视图和单选按钮中的答案选项。我必须在单击“下一步”按钮时将单选按钮值保存在共享首选项中,当我调用“上一步”按钮时,它会从“下一步”按钮获取共享首选项值,并在调用“上一步”按钮时将其设置为单选按钮。但在运行应用程序后,单选按钮值会保存在共享首选项中。但当我调用“上一步”按钮时,它不会获取共享首选项值,但不显示选中的单选按钮。有人能帮我吗?谢谢 这是我的活动代码Java 如何使用收音机组的共享首选项,java,android,Java,Android,我在“下一步”和“上一步”中有两个按钮,问题文本视图和单选按钮中的答案选项。我必须在单击“下一步”按钮时将单选按钮值保存在共享首选项中,当我调用“上一步”按钮时,它会从“下一步”按钮获取共享首选项值,并在调用“上一步”按钮时将其设置为单选按钮。但在运行应用程序后,单选按钮值会保存在共享首选项中。但当我调用“上一步”按钮时,它不会获取共享首选项值,但不显示选中的单选按钮。有人能帮我吗?谢谢 这是我的活动代码 read_Questions_XML_File(); button
read_Questions_XML_File();
button_Previouse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
previousQuestionCalled(v);
}
});
button_Next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nextQuestionCalled(v);
}
});
public void nextQuestionCalled(View view)
{
int id = Options_RadioGroup.getCheckedRadioButtonId();
System.out.println("id = " + id);
if (id > 0)
{
Options_RadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup R_Group, int checked_ID) {
// TODO Auto-generated method stub
RadioButton radioButton = (RadioButton)R_Group. findViewById(checked_ID);
int checkedIndex = R_Group.indexOfChild(radioButton);
System.out.println("checkedIndex = " + checkedIndex);
SharedPreferences sharedPreferences = getSharedPreferences("RGROUP_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("check", checkedIndex);
editor.commit();
}
});
//if animationForward == false
if (animationForward)
{
storingResult();
boolean flag;
if (animationForward)
{ flag = false; }
else
{ flag = true; }
animationForward = flag;
}
//Return The Number Of Element in this Vector > 0
//-1 + 1
if (-1 + Vectore_mquestionDatabaseStructure.size() > StaticClass.QuestionNumber)
{
if (StaticClass.isTest)
{
StaticClass.resultOfTest_Out_OF = 1 + StaticClass.resultOfTest_Out_OF;
}
StaticClass.QuestionNumber = 1 + StaticClass.QuestionNumber;
reHitting();
view.clearAnimation();
if (!StaticClass.isTest) {
button_QuestionLimit.clearAnimation();
}
if (StaticClass.isTest) {
button_QuestionLimit.clearAnimation();
}
return;
}
else
{
button_QuestionLimit.startAnimation(mAnimation);
return;
}
}
else if (id == -1)
{
Toast.makeText(this, "Please Select Any Option", Toast.LENGTH_LONG)
.show();
}
}
public void previousQuestionCalled(View view)
{
System.out.println("_________________in Previous Question____________");
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
int savedRadioIndex = sharedPreferences.getInt("check", 0);
System.out.println("savedRadioIndex is "+savedRadioIndex);
RadioButton savedCheckedRadioButton = (RadioButton)Options_RadioGroup.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
System.out.println("_________________in Previous Question____________");
if (!animationForward)
{
boolean flag;
if (animationForward)
{
flag = false;
}
else
{
flag = true;
}
animationForward = flag;
}
if (StaticClass.QuestionNumber > 0)
{
if (!StaticClass.isTest)
{
StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
reHitting();
button_QuestionLimit.clearAnimation();
}
view.clearAnimation();
}
else
{
button_QuestionLimit.startAnimation(mAnimation);
}
}
当我按下下一步按钮时,将共享的Prefs值保存在=int checkedIndex=R_Group.indexOfChild(radioButton)中
但当我按下这里的上一个按钮时,我得到的共享Prefs值是零或-1,单选按钮是未选中的
这是我的日志信息
10-08 16:47:54.007: I/System.out(459): id = 2131165205
10-08 16:47:54.007: I/System.out(459): strLimit = 2/12
10-08 16:47:54.007: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.016: I/System.out(459): checkedIndex = 2
10-08 16:47:54.086: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.086: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.086: I/System.out(459): checkedIndex = -1
10-08 16:47:54.096: I/System.out(459): ************************IN Next Button******************
10-08 16:48:06.756: I/System.out(459): _________________in Previous Question____________
10-08 16:48:06.756: I/System.out(459): savedRadioIndex is -1
在这之后,我检查了来自DDMS的xml文件,它不存储该值n,它还存储-1值,这里log cat显示了共享的prefs值
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="check" value="-1" />
</map>
在RadioGroup中,您必须在尝试保存时创建localInstance,以获取RadioGroup中当前选定的选项。我创建了最简单的示例和 您可以按如下所述实施:
private void saveRadioChoice(){
SharedPreferences mSharedPref = getSharedPreferences(MY_PREF_KEY,MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPref.edit();
// Initialize Radiogroup while saving choices
RadioGroup localRadioGroup = (RadioGroup) findViewById(R.id.choices);
editor.putInt(my_choice_key, localRadioGroup.indexOfChild(findViewById(localRadioGroup.getCheckedRadioButtonId())));
editor.apply();
}
保存此值后,可以按如下方式检索它
private void retrieveChoices(){
SharedPreferences sharedPref = getSharedPreferences(MY_PREF_KEY,MODE_PRIVATE);
int i = sharedPref.getInt(my_choice_key,-1);
if( i >= 0){
((RadioButton) ((RadioGroup)findViewById(R.id.myChoiceViewRadio)).getChildAt(i)).setChecked(true);
}
}
有人能帮帮我吗。有人能帮帮我吗!!!!!