Android 如何在单个 AlertDialog 上添加多个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12244297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add multiple buttons on a single AlertDialog
提问by Maddy
I have a butoon, on clicking of this button i want to open multiple buttons on a single AlertDialog like this :
我有一个按钮,单击此按钮时,我想在单个 AlertDialog 上打开多个按钮,如下所示:
Give Me a help :
给我一个帮助:
I was using this.... to add multiple buttons alertDialog.setButton(delete, "Delete", new OnClickListener() {
我正在使用这个.... 添加多个按钮 alertDialog.setButton(delete, "Delete", new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
but I found..., change setButton() to setButton2().. something like..... wt xcan i do for this....
但我发现...,将 setButton() 更改为 setButton2().. 类似..... wt xcan 我为此而做....
采纳答案by Carnal
I would inflate the AlertDialog with my own custom view (my_alert_dialog.xml).
我会用我自己的自定义视图 (my_alert_dialog.xml) 来扩充 AlertDialog。
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();
回答by Oded Breiner
A simple solution without xml:
一个没有 xml的简单解决方案:
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", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show();
break;
}
}
});
builder.create().show();
回答by Yalla T.
You can only create a Alertdialog with 3 buttons if you dont make the view by yourself.
如果您不自己制作视图,则只能创建带有 3 个按钮的 Alertdialog。
You can either make your own custom view in xml.
您可以在 xml 中创建自己的自定义视图。
but i'd suggest you just make a List.
但我建议你只做一个清单。
Check http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog"Adding a list"
检查 http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog“添加列表”
回答by MAC
Dialog dialog = new Dialog(context);
RelativeLayout featureLayout = (RelativeLayout) View.inflate(this, R.layout.yourview, null);
dialog.setContentView(featureLayout);
dialog.show();
回答by Manikandan K
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");
}
}