java - 如何在此徽标测验中添加更多随机问题?
问题描述
我本周创建了一个徽标测验应用程序,但这个徽标测验的问题在于它只有一个问题。回答后,测验结束。我想知道如何在此测验中添加更多问题以及如何随机显示问题。我的意思是,在回答完问题后,出现了另一张随机图片。我是 android 开发的新手,我仍然需要其他开发人员的帮助,我相信这是最好的提问地点。非常感谢您提前帮助我。
public class MainActivity extends AppCompatActivity {
private int presCounter = 0;
private int maxPresCounter = 5;
private String[] keys = {"S", "A", "M", "P", "L"};
private String textAnswer = "SAMPL";
TextView textScreen, textQuestion, textTitle;
@Override
protected void onCreate....
keys = shuffleArray(keys);
for (String key : keys) {
addView(((LinearLayout) findViewById(R.id.layoutParent)), key, ((EditText) findViewById(R.id.editText)));
}
maxPresCounter = 5;
}
private String[] shuffleArray(String[] ar) {
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
String a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
return ar;
}
private void addView(LinearLayout viewParent, final String text, final EditText editText) {
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
linearLayoutParams.rightMargin = 30;
final TextView textView = new TextView(this);
textView.setLayoutParams(linearLayoutParams);
textView.setBackground(this.getResources().getDrawable(R.drawable.pix));
textQuestion = (TextView) findViewById(R.id.textQuestion);
textScreen = (TextView) findViewById(R.id.textScreen);
textTitle = (TextView) findViewById(R.id.textTitle);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(presCounter < maxPresCounter) {
if (presCounter == 0)
editText.setText("");
editText.setText(editText.getText().toString() + text);
presCounter++;
if (presCounter == maxPresCounter)
doValidate();
}
}
});
viewParent.addView(textView);
}
private void doValidate() {
presCounter = 0;
EditText editText = findViewById(R.id.editText);
LinearLayout linearLayout = findViewById(R.id.layoutParent);
if(editText.getText().toString().equals(textAnswer)) {
Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();
editText.setText("");
} else {
Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
editText.setText("");
}
keys = shuffleArray(keys);
linearLayout.removeAllViews();
for (String key : keys) {
addView(linearLayout, key, editText);
}
}
}
解决方案
推荐阅读
- python - x 代表 mylist 中的 x 如果 x 不在 myNames 中 - 代表字母
- react-native - react-native-video 需要时间来加载实时流
- powershell - 将 Int32 中的到期日期转换为 Powershell 中的日期
- python - django 504 中的 cv2 网关超时
- c# - 为什么我的循环没有在 Release 中退出但在 Debug 中退出?
- amazon-web-services - 在 AWS Sagemaker 上使用 TF-serving-container 部署 TF2.1 模型后 ping 健康检查失败
- python - 优化 Python 3 中双/嵌套 for 循环的速度/时间复杂度/可扩展性:itertools、并行化、多处理、并发
- javascript - 它给了我一个错误,说 cart.map() 不是函数
- django - 如何使用 django 模型 clean() 方法?
- java - Java 11 中的泛型有什么问题吗?