
本文旨在解决Android Quiz App开发中,用户自定义问题数量时循环无法达到预期次数的问题。通过分析问题代码,提供简化后的解决方案,确保循环能够按照用户设定的数量执行,并成功存储问题和答案,最后跳转到答题页面。
原始代码中,循环逻辑存在一些问题,导致无法按照用户设定的问题数量进行循环。主要问题在于:
以下是修改后的 logicg() 方法,它解决了上述问题,并简化了代码:
private void logicg() {
String mount = jshared.getString("amount", "");
int amounts = Integer.parseInt(mount);
for (int i = 0; i < amounts; i++) {
map = new HashMap<>();
map.put("answer", Objects.requireNonNull(ans.getText()).toString());
map.put("question", inop.getText().toString());
jshared2.edit().putString("data", new Gson().toJson(map)).apply(); // 使用 apply 异步提交
}
Intent intent = new Intent(question.this, answerscrn.class);
startActivity(intent);
}代码解释:
注意事项:
完整示例代码:
package com.prgr.quizards.canary;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.AppCompatButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Objects;
public class question extends Activity {
private HashMap<String, Object> map = new HashMap<>();
private TextView text;
private EditText inop;
private TextInputEditText ans;
private AppCompatButton btn;
private SharedPreferences jshared2;
private SharedPreferences jshared;
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.activity_question);
initializedata();
AppCompatButton btn2 = findViewById(R.id.button3);
btn = findViewById(R.id.button);
btn2.setOnClickListener(v -> gotoback());
btn.setOnClickListener(view -> logicg());
}
public void gotoback(){
Intent intent = new Intent(question.this, activity_home_screen.class);
startActivity(intent);
}
private void initializedata(){
jshared = getSharedPreferences("j", Activity.MODE_PRIVATE);
jshared2 = getSharedPreferences("j2", Activity.MODE_PRIVATE);
inop = findViewById(R.id.inop);
ans = findViewById(R.id.ans);
text = findViewById(R.id.text1);
}
private void logicg() {
String mount = jshared.getString("amount", "");
int amounts = Integer.parseInt(mount);
for (int i = 0; i < amounts; i++) {
String question = inop.getText().toString();
String answer = Objects.requireNonNull(ans.getText()).toString();
if (TextUtils.isEmpty(question) || TextUtils.isEmpty(answer)) {
Toast.makeText(getApplicationContext(), "Question or Answer cannot be empty", Toast.LENGTH_SHORT).show();
return; // 停止执行,提示用户输入
}
map = new HashMap<>();
map.put("answer", answer);
map.put("question", question);
jshared2.edit().putString("data", new Gson().toJson(map)).apply();
}
Intent intent = new Intent(question.this, answerscrn.class);
startActivity(intent);
}
}通过修改循环条件,移除不必要的判断,并使用 apply() 方法异步提交数据,可以解决Android Quiz App开发中用户自定义问题数量时循环无法达到预期次数的问题。同时,需要注意输入校验和数据存储方式的选择,以确保程序的稳定性和性能。
以上就是Android Quiz App开发:解决用户自定义问题数量循环问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号