如何在一个AlertDialog上添加多个button

我有一个button,点击这个button,我想打开一个AlertDialog像这样的多个button: 在这里输入图像说明

给我一个帮助:

我正在使用此….添加多个buttonalertDialog.setButton(删除,“删除”,新的OnClickListener(){

public void onClick(View v) { // TODO Auto-generated method stub } }); 

但我发现…,更改setButton()setButton2()..类似….. wt xcan我做这个….

Solutions Collecting From Web of "如何在一个AlertDialog上添加多个button"

我会夸大AlertDialog与我自己的自定义视图(my_alert_dialog.xml)。

 AlertDialog.Builder alert = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); //inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..) View view = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) findViewById(R.id.root)); Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4. alert.setView(view); alert.show(); 

没有xml的简单解决scheme:

  AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Title"); builder.setItems(new CharSequence[] {"button 1", "button 2", "button 3", "button 4"}, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // The 'which' argument contains the index position // of the selected item switch (which) { case 0: Toast.makeText(context, "clicked 1", 0).show(); break; case 1: Toast.makeText(context, "clicked 2", 0).show(); break; case 2: Toast.makeText(context, "clicked 3", 0).show(); break; case 3: Toast.makeText(context, "clicked 4", 0).show(); break; } } }); builder.create().show(); 

如果您不自己制作视图,则只能使用3个button创buildAlertdialog。

您可以在xml中创build自己的自定义视图。

但我build议你只是做一个列表。

检查http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog “添加列表”

 Dialog dialog = new Dialog(context); RelativeLayout featureLayout = (RelativeLayout) View.inflate(this, R.layout.yourview, null); dialog.setContentView(featureLayout); dialog.show(); 
 int item = 0; ArrayList<String> list = new ArrayList<String>(); ArrayList<Integer> intList = new ArrayList<Integer>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); item = -1; for (int i = 0; i < list.size(); i++) { item++; intList.add(i); } showRatingBarAlertDialog(intList); private void showRatingBarAlertDialog(ArrayList<Integer> Id) { if (item != -1) { RatingFragment alertDialog = RatingFragment.instance(BaseActivity.this, Id.get(item), (ratingValue, comments) -> { CXLog.d(TAG, "select the rating::" + ratingValue); CXLog.d(TAG, "comment the feednback " + comments); item--; showRatingBarAlertDialog(requestId); }); alertDialog.show(CXBaseActivity.this.getFragmentManager(), "alertDialog"); } }