带alertDialog的单选按钮

时间:2014-01-07 14:57:48

标签: android

我的radiobuttons存在问题,因为它在myLayout中显示的是无线电组而不是在alertDialog中,我试图这样做,但我无法弄清楚如何放置它,因为我在android中是全新的。

   View myView = getLayoutInflater().inflate(R.layout.radiogroup, null);
    setContentView(myView);
    RadioGroup myRadioGroup = (RadioGroup)
    myView.findViewById(R.id.radioGroup);

    int radioGroupId = myRadioGroup.getCheckedRadioButtonId();
    RadioButton myCheckedButton = (RadioButton)
    myView.findViewById(radioGroupId);
    int index = myRadioGroup.indexOfChild(myCheckedButton);

    new AlertDialog.Builder(this)   
    .setMessage("Choose Startup Color:")
    .setPositiveButton("Set Startup Color", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
        }
     })

    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
        }
     })

    .show();

1 个答案:

答案 0 :(得分:0)

您正在设置活动的布局以通过执行

来显示RadioGroup布局
View myView = getLayoutInflater().inflate(R.layout.radiogroup, null);
setContentView(myView);

如果要在AlertDialog中显示View,可以通过调用构建器上的setView并将myView作为参数传递来设置它。