【发布时间】:2016-11-15 18:03:40
【问题描述】:
我正在使用一个对话框来显示 Android 中的一系列测验。一旦用户回答“真”或“假”,我想显示下一个测验。
我正在使用自定义布局,并使用以下代码对其进行控制:
public void showQuizDialog() {
View v;
TextView question;
// Textview showing Quiz 1 out of 3
TextView numQ;
NetworkImageView image;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// The coordinator keeps track of the game
coordinator.setQuizStarted();
// Get the quiz using the coordinator
Quiz qX = coordinator.getQuiz();
final String answer = qX.getAnswer();
final String quizTracker = coordinator.getTracker();
// Create an instance of the dialog fragment
AlertDialog.Builder quiz = new AlertDialog.Builder(MyActivity.this);
LayoutInflater inflater = MyActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
v = inflater.inflate(R.layout.quiz_layout,null);
// Set the question
question = (TextView) v.findViewById(R.id.content_quiz);
question.setText(qX.getQuestion());
// Set current number of question
numQ = (TextView) v.findViewById(R.id.number_quiz);
numQ.setText(quizTracker);
// Retrieves the image from url
image = (NetworkImageView) v.findViewById(R.id.thumbnail_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String urlImage = getCompleteUrl(CODE_QUIZ_IMG);
urlImage += qX.getImg();
image.setImageUrl(urlImage, imageLoader);
quiz.setPositiveButton("True", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
if (answer.equals(quiz_true)) {
coordinator.notifyCorrectQuizNum();
Log.d(QUIZ_DIALOG,"\t\tCorrect answer! it was true");
}
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
}
})
.setNegativeButton("False", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked to False btn
if (answer.equals(quiz_false)){
Log.d(QUIZ_DIALOG,"\t\tCorrect Answer!! It was false");
coordinator.notifyCorrectQuizNum();
}
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
}
});
quiz.setView(v);
final AlertDialog dialog = quiz.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
假设有 100 个测验要显示,并且 Coordinator 类的一个方法返回剩余测验的数量。
所以,通话结束后
coordinator.setQuizEnded(true);
coordinator.updateQuizLeft();
coordinator.getQuizLeft() 将返回 99。coordinator.getQuiz() 将指向下一个测验。
如何在不关闭对话框的情况下更新TextView question 和numQ?
【问题讨论】:
-
感谢@Andriy Omelchenko 的标签编辑!
标签: android dialog android-custom-view updates android-dialog