AlertDialog 在没有处理程序的情况下获取返回值
我想显示一些警报对话框,因此用户必须处理一些问题(有点像向导)。
是否可以让alertDialog等待用户选择某些内容,然后返回选择的值?
HashMap<Integer, Question> questions = DataConnector.getCallQuestions(position);
int nextQuestion = position;
while(nextQuestion != 0){
Question question = questions.get(nextQuestion);
CharSequence[] items = (String[])question.getAnswers();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(question.getQuestion());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
AlertDialog alert = builder.create();
//I would like to do something like this:
nextQuestion = alert.getClickedItem();
}
编辑。回复 chris:
程序的执行应该等到用户选择alertDialog 中的选项之一
这可能吗?
像这样:
private int answer = 0;
....
...methode(){
//show dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(question.getQuestion());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
CallHandleMenu.this.answer = item; //setting answer
}
});
builder.create().show();
//Here I need to know the answer (the answer should not be 0 here)
I would like to show a number of alertdialogs, so the user has to handle off some questions (a little like a wizard).
Is it possible to make the alertDialog wait until the user chooses something and then returning the choisen value?
HashMap<Integer, Question> questions = DataConnector.getCallQuestions(position);
int nextQuestion = position;
while(nextQuestion != 0){
Question question = questions.get(nextQuestion);
CharSequence[] items = (String[])question.getAnswers();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(question.getQuestion());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
AlertDialog alert = builder.create();
//I would like to do something like this:
nextQuestion = alert.getClickedItem();
}
EDIT. Reponse to chris:
The execution of the program should wait until the user chooses one of the options in the alertDialog
Is this possible?
Like this:
private int answer = 0;
....
...methode(){
//show dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(question.getQuestion());
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
CallHandleMenu.this.answer = item; //setting answer
}
});
builder.create().show();
//Here I need to know the answer (the answer should not be 0 here)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望出现一系列
AlertDialog
框,那么最好的选择是在对话框本身内部设置一些侦听器。请注意,我使用警报对话框的默认肯定按钮,您可能需要根据用户单击的内容更改此按钮。
如果您要从
AlertDialog
onClickListener
对Activity
类进行一些回调,您可以使用MyActivityClassName.this.myMethodOrVariableHere
这样做。希望有帮助,如果您需要更多说明,请给我留言。
If you want a series of
AlertDialog
boxes to appear then your best bet would be to set some listeners inside the dialogs themselves.Note that I am using the default positive button for the alert dialog, you may need to change this depending on what the user is clicking.
If you are going to be making some call backs into your
Activity
class from theAlertDialog
onClickListener
you can useMyActivityClassName.this.myMethodOrVariableHere
to do so.Hope that helps, leave me a comment if you want some more clarification.
我用这种方式修复了它(伪代码),因为我的代码很脏:p
I fixed it this way (pseudocode), because my code is to dirty :p