-1
我在Android應用程序上工作,這是一種用RadioButtons
進行的投票。 現在將選定的單選按鈕保存在SharedPreference
中。Android Studio:共享首選項保存到文件
這裏我的代碼:
public class frage1 extends AppCompatActivity {
public RadioGroup antworten_frage1;
public RadioButton antwort_frage1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frage1);
//initialize radio group
antworten_frage1 = (RadioGroup) findViewById(R.id.radio_antworten_frage1);
antworten_frage1.clearCheck();
//attach CheckedChangeListener to radio group
antworten_frage1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton antwort_frage1 = (RadioButton) group.findViewById(checkedId);
if(null!=antwort_frage1 && checkedId > -1){
Toast.makeText(frage1.this, antwort_frage1.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
//button_zurück
public void select_button_back1(View view)
{
Toast.makeText(this, "Es geht nicht Zurück. Das ist die erste Frage.", Toast.LENGTH_LONG).show();
}
//button_weiter
public void select_button_go1 (View view)
{
startActivity(new Intent(frage1.this, frage2.class));
//write answer
RadioButton antwort_frage1 = (RadioButton) antworten_frage1.findViewById(antworten_frage1.getCheckedRadioButtonId());
SharedPreferences sharedPreferences=getSharedPreferences("antworten", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("antwort_frage1",antwort_frage1.getText().toString());
editor.apply();
Toast.makeText(this, "Antwort gespeichert", Toast.LENGTH_SHORT).show();
}
}
現在我的問題: 我如何保存答案文件在設備上(這樣我就可以讀它以後在PC上)?
** 1 **。以json格式映射您的答案,將json寫入文件並將文件保存在內部存儲器中。 ** 2 **。使用類似** SQLite **的數據庫。 – AKSiddique