简体   繁体   English

如何将RadioGroup和EditText添加到AlertDialog?

[英]How to add RadioGroup and EditText to AlertDialog?

Hi I want to get user name and user gender using alert dialog so I add: 嗨,我想使用警报对话框获取用户名和用户性别,所以我添加:

AlertDialog.Builder user= new AlertDialog.Builder(this);
     user.setTitle("New Student");
     user.setMessage("What is your Name?");
     final RadioGroup genderRG= new RadioGroup(this);
     RadioButton radiomr = new RadioButton(this);
     radiomr.setText("Mr");
//   radiomr.setId(1);
     genderRG.addView(radiomr);
     RadioButton radiomiss = new RadioButton(this);
     radiomiss.setText("Miss");
//   radiomiss.setId(2);
     genderRG.addView(radiomiss);
     user.setView(genderRG);

     final EditText input = new EditText(this);
     user.setView(input);

     user.setPositiveButton("OK", new DialogInterface.OnClickListener()
         {
             @Override
             public void onClick(DialogInterface dialog, int whichButton)
             {
             //  get data                          
             }
         });
     user.setNegativeButton("CANCEL", null);
     user.create().show();

But when I run it, It just gives me an EditText. 但是当我运行它时,它只是给我一个EditText。 Please! 请! How can I get Gender and Name in same AlertDialog using radio button or spinner. 如何使用单选按钮或微调器在同一AlertDialog中获取性别和名称。

You should create your own layout and set it via setView() . 您应该创建自己的布局并通过setView() You are instead inflating just the EditText, this is why you have just that in the dialog. 相反,您只是夸大了EditText,这就是为什么对话框中只有它。

In any case it is better to use DialogFragment to create your own dialog version. 无论如何,最好使用DialogFragment创建自己的对话框版本。

More here https://developer.android.com/reference/android/app/DialogFragment 此处更多https://developer.android.com/reference/android/app/DialogFragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM