Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

menu - Android: create a popup that has multiple selection options

I've been searching around trying to figure out how to create a popup or a dialog that has 4 options to choose from.

I see this picture on the Android developer site:

enter image description here

Does anyone know how to code up something like the one on the right? I don't need any icons next to my text, I just need to be able to select from 4 options.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a String array with the options you want to show there and then pass the array to an AlertDialog.Builder with the method setItems(CharSequence[], DialogInterface.OnClickListener).

An example:

String[] colors = {"red", "green", "blue", "black"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // the user clicked on colors[which]
    }
});
builder.show();

The output (on Android 4.0.3):

Output

(Background map not included. ;))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...