Android 如何在测验中使用共享首选项
我面临着一个共同偏好的问题 在我的应用程序中,我使用共享首选项保存测验答案 我试过了。当我问perivous的问题时,它很好但是我的问题是 当第一个问题出现时,用户选择一个答案并进入第二个问题,在这里用户也将选择答案并进入第三个问题,就像明智的过程一样。。当用户返回perivous问题(第二个问题)时,这里的用户回答会正确检查按钮,而当用户此时进入下一个问题时,答案不会正确检查。。。我做错了什么 请指给我看。。并给出一个解决方案,如何得到它?先谢谢你 对不起,我英语不好 这是我的密码Android 如何在测验中使用共享首选项,android,radio-button,sharedpreferences,Android,Radio Button,Sharedpreferences,我面临着一个共同偏好的问题 在我的应用程序中,我使用共享首选项保存测验答案 我试过了。当我问perivous的问题时,它很好但是我的问题是 当第一个问题出现时,用户选择一个答案并进入第二个问题,在这里用户也将选择答案并进入第三个问题,就像明智的过程一样。。当用户返回perivous问题(第二个问题)时,这里的用户回答会正确检查按钮,而当用户此时进入下一个问题时,答案不会正确检查。。。我做错了什么 请指给我看。。并给出一个解决方案,如何得到它?先谢谢你 对不起,我英语不好 这是我的密码 publi
public class MainActivity extends Activity {
RadioGroup btn_practicerg;
RadioButton btn_practice1;
RadioButton btn_practice2;
RadioButton btn_practice3;
RadioButton btn_practice4;
RadioGroup radioButton;
TextView quetxt;
Button next;
Button previous;
int i = 0, k = 0;
int checkedIndex;
int num = 1;
private ArrayList<String> answ1 = new ArrayList<String>();
private ArrayList<String> ques1 = new ArrayList<String>();
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_practicerg = (RadioGroup) findViewById(R.id.rdgroup);
btn_practice1 = (RadioButton) findViewById(R.id.RB1);
btn_practice2 = (RadioButton) findViewById(R.id.RB2);
btn_practice3 = (RadioButton) findViewById(R.id.RB3);
btn_practice4 = (RadioButton) findViewById(R.id.RB4);
quetxt = (TextView) findViewById(R.id.que_txt);
next = (Button) findViewById(R.id.nxt_btn);
previous = (Button) findViewById(R.id.accbtn);
runOnUiThread(new Runnable() {
public void run() {
LoadQuestions();
}
});
next.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
int selectedId = btn_practicerg.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton radioSexButton = (RadioButton) findViewById(selectedId);
checkedIndex = btn_practicerg.indexOfChild(radioSexButton);
Toast.makeText(MainActivity.this, radioSexButton.getText(),
Toast.LENGTH_SHORT).show();
if (i == ques1.size() - 1) {
showAlert();
} else {
++i;
++num;
TextView quetxt = (TextView) findViewById(R.id.que_txt);
quetxt.setText("Q" + num + ")" + ques1.get(i));
++k;
btn_practice1.setText(answ1.get((k * 4) + 0));
btn_practice2.setText(answ1.get((k * 4) + 1));
btn_practice3.setText(answ1.get((k * 4) + 2));
btn_practice4.setText(answ1.get((k * 4) + 3));
btn_practicerg.clearCheck();
SavePreferences(String.valueOf(num), checkedIndex);
}
}
private void showAlert() {
// TODO Auto-generated method stub
}
});
Button previousbtn1 = (Button) findViewById(R.id.accbtn);
previousbtn1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
--i;
--num;
TextView quetxt = (TextView) findViewById(R.id.que_txt);
quetxt.setText("Q" + num + ")" + ques1.get(i));
--k;
btn_practice1.setText(answ1.get((k * 4) + 0));
btn_practice2.setText(answ1.get((k * 4) + 1));
btn_practice3.setText(answ1.get((k * 4) + 2));
btn_practice4.setText(answ1.get((k * 4) + 3));
LoadPreferences();
}
});
}
private void LoadQuestions() {
ques1.add("whats the name?");
ques1.add("whats place?");
ques1.add("whats the favourite?");
ques1.add("whats the game?");
ques1.add("whats the time?");
answ1.add("A");
answ1.add("B");
answ1.add("C");
answ1.add("D");
answ1.add("MDU");
answ1.add("MS");
answ1.add("CHE");
answ1.add("POND");
answ1.add("1");
answ1.add("2");
answ1.add("3");
answ1.add("4");
answ1.add("VB");
answ1.add("TENN");
answ1.add("HOC");
answ1.add("CRI");
answ1.add("11");
answ1.add("12");
answ1.add("13");
answ1.add("14");
quetxt = (TextView) findViewById(R.id.que_txt);
quetxt.setText("Q" + num + ")" + ques1.get(i));
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
}
private void SavePreferences(String key, int value) {
int quest = (Integer.parseInt(key)) - 1;
SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",
0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(String.valueOf(quest), value);
editor.commit();
}
private void LoadPreferences() {
SharedPreferences getProgramPrefs = this.getSharedPreferences(
"MY_SHARED_PREF", MODE_WORLD_READABLE);
int savedRadioIndex = getProgramPrefs.getInt(String.valueOf(num), -1);
System.out.println("SavedRadioIndex" + savedRadioIndex);
RadioButton savedCheckedRadioButton = (RadioButton) btn_practicerg
.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
}
OnCheckedChangeListener radioGroupOnCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) group
.findViewById(checkedId);
checkedIndex = group.indexOfChild(radioButton);
System.out.println("checkedIndex" + checkedIndex);
}
};
}
公共类MainActivity扩展活动{
放射组btn_实习医生;
无线按钮btn_练习1;
无线按钮btn_练习2;
无线按钮btn_练习3;
无线按钮btn_练习4;
放射组单选按钮;
文本视图;
按钮下一步;
按钮前;
int i=0,k=0;
int-checkedIndex;
int num=1;
private ArrayList answ1=新建ArrayList();
private ArrayList ques1=新的ArrayList();
最终字符串键保存单选按钮索引=“保存单选按钮索引”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_practicerg=(放射组)findViewById(R.id.rdgroup);
btn_practice1=(单选按钮)findViewById(R.id.RB1);
btn_practice2=(单选按钮)findViewById(R.id.RB2);
btn_practice3=(单选按钮)findViewById(R.id.RB3);
btn_practice4=(单选按钮)findViewById(R.id.RB4);
quetxt=(TextView)findViewById(R.id.que_txt);
next=(按钮)findViewById(R.id.nxt\u btn);
previous=(按钮)findviewbyd(R.id.accbtn);
runOnUiThread(新的Runnable(){
公开募捐{
加载问题();
}
});
next.setOnClickListener(new Button.OnClickListener(){
公共void onClick(视图v){
int selectedId=btn_practicerg.getCheckedRadioButtonId();
//通过返回的id查找radiobutton
RadioButton RadioExButton=(RadioButton)findViewById(selectedId);
checkedIndex=btn_practicerg.indexOfChild(RadioExButton);
Toast.makeText(MainActivity.this,RadioExButton.getText(),
吐司。长度(短)。show();
if(i==ques1.size()-1){
showarert();
}否则{
++一,;
++num;
TextView quetxt=(TextView)findViewById(R.id.que_txt);
quetxt.setText(“Q”+num+”)+ques1.get(i);
++k;
btn_practice1.setText(answ1.get((k*4)+0));
btn_practice2.setText(answ1.get((k*4)+1));
btn_practice3.setText(answ1.get((k*4)+2));
btn_practice4.setText(answ1.get((k*4)+3));
btn_practicerg.clearCheck();
SavePreferences(String.valueOf(num),checkedIndex);
}
}
私有void showAlert(){
//TODO自动生成的方法存根
}
});
按钮previousbtn1=(按钮)findViewById(R.id.accbtn);
previousbtn1.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
--一,;
--num;
TextView quetxt=(TextView)findViewById(R.id.que_txt);
quetxt.setText(“Q”+num+”)+ques1.get(i);
--k;
btn_practice1.setText(answ1.get((k*4)+0));
btn_practice2.setText(answ1.get((k*4)+1));
btn_practice3.setText(answ1.get((k*4)+2));
btn_practice4.setText(answ1.get((k*4)+3));
LoadPreferences();
}
});
}
私人问题(){
问题1.添加(“名称是什么?”);
问题1.添加(“什么地方?”);
问题1.添加(“最喜欢的是什么?”);
问题1.添加(“游戏是什么?”);
问题1.加上(“几点了?”);
answ1.添加(“A”);
answ1.添加(“B”);
answ1.添加(“C”);
answ1.添加(“D”);
answ1.添加(“MDU”);
answ1.添加(“MS”);
answ1.添加(“CHE”);
answ1.添加(“池塘”);
answ1.添加(“1”);
answ1.添加(“2”);
answ1.添加(“3”);
answ1.添加(“4”);
answ1.添加(“VB”);
answ1.添加(“田纳西”);
answ1.添加(“HOC”);
answ1.添加(“CRI”);
answ1.添加(“11”);
answ1.添加(“12”);
answ1.添加(“13”);
answ1.添加(“14”);
quetxt=(TextView)findViewById(R.id.que_txt);
quetxt.setText(“Q”+num+”)+ques1.get(i);
btn_practice1.setText(answ1.get(0));
btn_practice2.setText(answ1.get(1));
btn_practice3.setText(answ1.get(2));
btn_practice4.setText(answ1.get(3));
}
私有void SavePreferences(字符串键,int值){
intquest=(Integer.parseInt(key))-1;
SharedReferences首选项=获取SharedReferences(“我的共享首选项”,
0);
SharedReferences.Editor=首选项.edit();
editor.putInt(String.valueOf(quest),value);
commit();
}
私有void LoadPreferences(){
SharedReferences getProgramPrefs=this.GetSharedReferences(
“我的共享”模式(世界可读);
int savedRadioIndex=getProgramPrefs.getInt(String.valueOf(num),-1);
System.out.println(“SavedRadioIndex”+SavedRadioIndex);
RadioButton savedCheckedRadioButton=(RadioButton)btn_practicerg
.getChildAt(savedRadioIndex);
拯救
public class Question {
private int id;
private String question;
private List<Answer> possibleAnswers;
private int correctAnswerId;
private String userAnswer;
}
private void SavePreferences(String key, int value) {
SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
editor.commit();
}