简体   繁体   English

如何以编程方式确保用户已使用AlertDialog.Builder选择了一个选项?

[英]How can I programmatically make sure a user has selected an option with AlertDialog.Builder?

In my android application, I have created a dialog box showing the user multiple options from a list. 在我的android应用程序中,我创建了一个对话框,向用户显示列表中的多个选项。

I have used this class to do so: http://developer.android.com/reference/android/app/AlertDialog.Builder.html 我使用此类来做到这一点: http : //developer.android.com/reference/android/app/AlertDialog.Builder.html

More specifically, I used setMultiChoiceItems 更具体地说,我使用了setMultiChoiceItems

Now the options are displaying correctly and everything works well. 现在,选项正确显示,并且一切正常。 However, I need to make sure that the user has actually selected one of these options and not immediately pressed the proceeding Button that I have set using setPositiveButton. 但是,我需要确保用户实际上已经选择了这些选项之一,并且没有立即按下使用setPositiveButton设置的前进按钮。

Is there a way I can programmatically make sure one or more of these options is picked? 有什么方法可以通过编程确保选择一个或多个这些选项? I will set up a quick if statement as well and even display a warning dialog if none of the options is picked. 我还将设置一个快速的if语句,如果未选择任何选项,甚至会显示一个警告对话框。

Thank you and I apologize if this question has been asked before. 谢谢您,如果这个问题以前有人提出过,我深表歉意。

The easiest way to do it is to use the DialogInterface.OnMultiChoiceClickListener to keep track of the user's selections. 最简单的方法是使用DialogInterface.OnMultiChoiceClickListener来跟踪用户的选择。 Initialize an array before you create the dialog and then every time the user makes/clears a selection then add/remove it from the array. 在创建对话框之前初始化一个数组,然后每次用户进行选择/清除选择,然后从数组中添加/删除它。 Once the user hits the positive button check if the array is empty: if it is, they made no selections. 用户单击肯定按钮后,检查数组是否为空:如果为空,则他们没有选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM