Android: create a popup that has multiple selection options

前端 未结 4 972
半阙折子戏
半阙折子戏 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:34

    Try this :

    public void onClick(View v) {
    
        final String[] fonts = {
            "Small", "Medium", "Large", "Huge"
        };
    
        AlertDialog.Builder builder = new AlertDialog.Builder(TopicDetails.this);
        builder.setTitle("Select a text size");
        builder.setItems(fonts, new DialogInterface.OnClickListener() {@
            Override
            public void onClick(DialogInterface dialog, int which) {
                if ("Small".equals(fonts[which])) {
                    Toast.makeText(TopicDetails.this, "you nailed it", Toast.LENGTH_SHORT).show();
                } else if ("Medium".equals(fonts[which])) {
                    Toast.makeText(TopicDetails.this, "you cracked it", Toast.LENGTH_SHORT).show();
                } else if ("Large".equals(fonts[which])) {
                    Toast.makeText(TopicDetails.this, "you hacked it", Toast.LENGTH_SHORT).show();
                } else if ("Huge".equals(fonts[which])) {
                    Toast.makeText(TopicDetails.this, "you digged it", Toast.LENGTH_SHORT).show();
                }
                // the user clicked on colors[which]
    
            }
        });
        builder.show();
    }
    

提交回复
热议问题