0
這是應用視圖:
如何使用SharedPreference保存RadioButton狀態?
,這裏是我的活動代碼:
public class Quiz extends AppCompatActivity {
public static int point;
public static int i = 0;
RadioGroup radioGroup;
public static int totalPoint =0;
int arrValues[]={0,0,0,0};
int iPosition=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
i = 0;
final RadioButton radioButton1 =(RadioButton) findViewById(R.id.number_1);
final RadioButton radioButton2 =(RadioButton) findViewById(R.id.number_2);
final RadioButton radioButton3 =(RadioButton) findViewById(R.id.number_3);
final RadioButton radioButton4 =(RadioButton) findViewById(R.id.number_4);
final String[] items = getResources().getStringArray(R.array.quiz);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
Button nextButton = (Button) findViewById(R.id.button);
Button previousButton = (Button) findViewById(R.id.button2);
radioButton1.setText(items[0]);
radioButton2.setText(items[1]);
radioButton3.setText(items[2]);
radioButton4.setText(items[3]);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) throws ArrayIndexOutOfBoundsException {
try {
if (checkedId == R.id.number_1) {
arrValues[iPosition] = 0;
} else if (checkedId == R.id.number_2) {
arrValues[iPosition] = 1;
}else if (checkedId == R.id.number_3) {
arrValues[iPosition]= 2;
}else if (checkedId == R.id.number_4) {
arrValues[iPosition] = 3;
}
// Total
totalPoint = getTotalPoint();
Log.e("sd",""+totalPoint);
}
catch (ArrayIndexOutOfBoundsException sd){
Log.e("sd","sdsd");
}
}
private int getTotalPoint() {
int sum = 0;
for (int i = 0; i < arrValues.length; i++)
sum += arrValues[i];
Log.e("sum",""+sum);
return sum;
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadNextQuestion();
}
private void loadNextQuestion() throws ArrayIndexOutOfBoundsException {
// Update UI
// Clear radio button state
try {
if (i>= items.length-4){
return;
}
iPosition++;
i = i + 4;
radioGroup.clearCheck();
radioButton1.setText(items[i]);
radioButton2.setText(items[i + 1]);
radioButton3.setText(items[i + 2]);
radioButton4.setText(items[i + 3]);
} catch (ArrayIndexOutOfBoundsException arr) {
}
}
});
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Update UI
// Clear radio button state
loadpreQuestion();
}
private void loadpreQuestion() {
try {
if (i<=0){
return;
}
iPosition--;
i = i - 4;
radioGroup.clearCheck();
radioButton1.setText(items[i]);
radioButton2.setText(items[i + 1]);
radioButton3.setText(items[i + 2]);
radioButton4.setText(items[i + 3]);
} catch (ArrayIndexOutOfBoundsException arr) {
}
}
});
}
public void onRadioButtonClicked(View view)throws ArrayIndexOutOfBoundsException {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
try {
switch(view.getId()) {
case R.id.number_1:
if (checked)
point = 0;
Toast.makeText(this, ""+ arrValues[iPosition] ,
Toast.LENGTH_SHORT).show();
// Pirates are the best
break;
case R.id.number_2:
if (checked)
point = 1;
Toast.makeText(this, ""+ arrValues[iPosition] ,
Toast.LENGTH_SHORT).show();
// Ninjas rule
break;
case R.id.number_3:
if (checked)
point = 2;
Toast.makeText(this, ""+arrValues[iPosition] ,
Toast.LENGTH_SHORT).show();
// Ninjas rule
break;
case R.id.number_4:
if (checked)
point = 3;
Toast.makeText(this, ""+ arrValues[iPosition] ,
Toast.LENGTH_SHORT).show();
// Ninjas rule
break; }
}
catch (ArrayIndexOutOfBoundsException ds){}
}
@Override
public void onBackPressed() {
super.onBackPressed();
i = 0;
}
}
正如你看到的只有4個單選按鈕的時間和他們的文字只是Next
和Previous
按鈕來改變。那麼如何在不影響其他問題的情況下保存RadioButton狀態與SharedPrefrence?
更新代碼:
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) throws ArrayIndexOutOfBoundsException {
try {
if (checkedId == R.id.number_1) {
arrValues[iPosition] = 0;
editor.putInt(String.valueOf(iPosition), 0);
} else if (checkedId == R.id.number_2) {
arrValues[iPosition] = 1;
editor.putInt(String.valueOf(iPosition), 1);
sharedPreferences.getInt(String.valueOf(iPosition),1);
int radioButtonStatusQ1 = sharedPreferences.getInt("0", 0);
if (radioButtonStatusQ1==1){
radioGroup.check(R.id.number_2);
}
}else if (checkedId == R.id.number_3) {
arrValues[iPosition]= 2;
editor.putInt(String.valueOf(iPosition), 2);
}else if (checkedId == R.id.number_4) {
arrValues[iPosition] = 3;
editor.putInt(String.valueOf(iPosition), 3);
}
// Commit
editor.commit();
// Total
totalPoint = getTotalPoint();
Log.e("sd",""+totalPoint);
}
catch (ArrayIndexOutOfBoundsException sd){
Log.e("sd","sdsd");
}
}
我要救的單選按鈕的不是價值的'checked'狀態它。 –
仍然保存狀態。例如:你的'問題1' >>如果你點擊'R.id.number_2' >>它存儲爲'editor.putInt(String.valueOf(iPosition),1);'這裏1代表你的單選按鈕' R.id.number_2'檢查。 – FAT
我想要使用類似於:'if(radioButtonStatusQ1 == 1){ radioGroup.check(R.id.number_2); }'但它不起作用 –