Android: create a popup that has multiple selection options

前端 未结 4 996
半阙折子戏
半阙折子戏 2020-12-04 11:33

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 sit

4条回答
  •  囚心锁ツ
    2020-12-04 12:24

    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. ;))

提交回复
热议问题