【发布时间】:2026-01-08 11:25:03
【问题描述】:
Intent intent1 = new Intent(Questions.this, Questions.class); startActivity(intent1);
我学习中的小问题。 对不起我的法语^^
一个变量每次都会改变,我按下一个按钮,它会备份并分配它++。
在按钮输入中,如果变量 == 在表 REPONSE.Length 中,它会重新启动活动并删除备份。
我的问题是,当活动重新启动良好时,备份不会自行删除。
每次我支持活动时,它都会再次引发,但无法在第 0 阶段重新开始。
int REPONSE[]= new int[5]; //tableau des reponses
int Question = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
Question = sharedPreferences.getInt("num", 0);
cardView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Restart si Question == REPONSE.length
if (Question == REPONSE.length){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove("num");
editor.apply();
Intent intent1 = new Intent(Questions.this, Questions.class);
startActivity(intent1);
}
//Sauvegarde de la variable
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("num", Question++);
editor.apply();
//Incrementation +1
Question++;
}
}); }
提前致谢:)
【问题讨论】: