Android - How to implement a listbox as a modal dialog/popup
In my Android app, when a user clicks on a button, I want a list box to show up as a modal dialog, just like the way a Spinner works, except that the list box can also allow multiple choices. I tried using ListView as described in the android hello-listview tutorial (http://developer.android.com/resources/tutorials/views/hello-listview.html). Unfortunately it doesn't seem to work the way I had expected it to. It doesn't show up as a modal dialog like the Spinner. I tried looking at what the Android browser does when a listbox is to be displayed. I browsed to www.functio开发者_如何学JAVAnx.com/html/lesson14.htm in the browser on my Android device and saw the following behavior (and this is exactly the behavior I want in my app):
When a dropdown/combobox in HTML is clicked, a spinner comes up as seen in the image here: http://img842.imageshack.us/img842/803/htmlcombobox.png When a single select listbox is clicked, again a spinner comes up as seen here: http://img13.imageshack.us/img13/3355/listboxsingle.png And when a multi-select listbox is clicked, a multi-select spinner / listview shows up in a dialog as seen here: http://img835.imageshack.us/img835/711/listboxmulti.png So my questions are:- What is this widget (in the last image above) that allows multi-select in a modal dialog. I'm sure this must be a component already available on the Android platform since it's being displayed in the browser.
- Even the Spinners (in the first 2 images) in the browser look different than the default Spinner I'm seeing in my app. Would the browser be applying custom skinning / colors to the background and text of the Spinners that it displays?
Here's the code in case anyone's interested:
new AlertDialog.Builder(this)
.setMultiChoiceItems(R.array.select_dialog_items,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
/* User clicked on a check box do some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.show();
Check the ApiDemos sample project, the AlertDialogSamples activity. There is a button labeled "Repeat alarm" that invokes a modal dialog with a multiple choice list.
In any case, a good place to start would be AlertDialog.Builder
.
精彩评论