带有单选按钮和validation按钮的对话框

在此处输入图像描述
我想在两个无线电第二单选按钮B下方添加一个按钮,当我选中一个选项并点击validation时,就会发生一个动作。 请帮忙

final CharSequence[] photo = {"A","B"}; AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Select Gender"); alert.setSingleChoiceItems(photo,-1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(photo[which]=="A") { gen="B"; } else if (photo[which]=="B") { gen="B"; } } }); alert.show(); 

制作自定义对话框的方法

http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application reference from this here

  1. 创建一个xml自定义对话框

           

和activity.java文件

  Dialog dialog = new Dialog(Dialogeshow.this); dialog.setContentView(R.layout.custom_dialoge); dialog.setTitle("This is my custom dialog box"); dialog.setCancelable(true); // there are a lot of settings, for dialog, check them all out! // set up radiobutton RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_); RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2); // now that the dialog is set up, it's time to show it dialog.show(); 

尝试这样做,你只需要选择默认选择并在对话框中添加一个整数 – > inputSelection

 final CharSequence[] items = { " HDMI IN ", " AV IN" }; // Creating and Building the Dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select Input Type"); builder.setSingleChoiceItems(items,inputSelection, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { inputSelection = item; levelDialog.dismiss(); } }); levelDialog = builder.create(); levelDialog.show(); 

您可以使用以下方法显示对话框

 public void showDialog(Context context, String title, String[] btnText, DialogInterface.OnClickListener listener) { final CharSequence[] items = { "One", "Two" }; if (listener == null) listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface paramDialogInterface, int paramInt) { paramDialogInterface.dismiss(); } }; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(title); builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { } }); builder.setPositiveButton(btnText[0], listener); if (btnText.length != 1) { builder.setNegativeButton(btnText[1], listener); } builder.show(); } 

调用部分可以按如下方式完成:

 showDialog(MainActivity.this, "Your Title", new String[] { "Ok" }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which==-1) Log.d("Neha", "On button click"); //Do your functionality here } }); 

您可以使用Builder.setNeutralButton向对话框添加单个按钮。