如何在对话框中添加多个文本视图

时间:2015-11-13 09:39:05

标签: android android-alertdialog

我想在对话框中添加多个TextView。我尝试了这个,但它只显示了一个TextView。

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here
                alert.setMessage("Enter Your Name Here"); //Message here
            for (int i =0; i<20; i++) {
                // Set an EditText view to get user input
                final TextView input = new TextView(context);
                input.setText("" + i);
                input.setPadding(5, 5, 5, 5);
                input.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String srt = "clickd";
                        Toast.makeText(context, srt, Toast.LENGTH_LONG).show();

                    }
                });
                alert.setView(input);
            }
                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        String srt = "Dialogg";
                        Toast.makeText(context, srt, Toast.LENGTH_LONG).show();
                    } // End of onClick(DialogInterface dialog, int whichButton)
                }); //End of alert.setPositiveButton
                alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                        dialog.cancel();
                    }
                }); //End of alert.setNegativeButton
                AlertDialog alertDialog = alert.create();
                alertDialog.show();

我想在对话框中显示1到20之间的数字。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

我要做的是创建线性布局,然后将文本视图添加到线性布局,然后将线性布局添加到对话框中。

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

for (int i =0; i<20; i++) {
            // Set an EditText view to get user input 
            final TextView input = new TextView(context);
            input.setText("" + i);
            input.setPadding(5, 5, 5, 5);
            input.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View v) {
                    String srt = "clickd";
                    Toast.makeText(context, srt, Toast.LENGTH_LONG).show();

                } 
            }); 
layout.addView(input); 

然后

alert.setView(layout); 

答案 1 :(得分:0)

您可以使用自定义提醒对话框。使用这种方法,您可以更好地控制警报对话框:

http://www.mkyong.com/android/android-custom-dialog-example/