AlertDialog:为什么我不能一起显示消息和列表?

时间:2014-03-21 13:08:32

标签: android android-alertdialog

当我使用AlertDialog添加消息时,是否有人知道为什么.setMessage()没有显示项目列表? 将显示负面和正面按钮,但不会显示列表。 当我删除.setMessage()行时,一切正常。

这是我的代码:

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");

CharSequence[] items = {"RED", "BLUE", "GREEN" };

myAlertDialog.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // do stuff
    }
});

myAlertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});

myAlertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});

myAlertDialog.create();
myAlertDialog.show();

5 个答案:

答案 0 :(得分:13)

来自docs

  

由于列表出现在对话框的内容区域中,因此对话框无法显示消息和列表,您应该使用setTitle()为对话框设置标题。

setMessage()setSingleChoiceItems()因此是互斥的。

答案 1 :(得分:4)

AlertDialog通过检查您通过mMessage设置的setMessage()是否为null来动态创建其内容。如果mMessage null(这意味着您调用了setMessage()方法),则会为 {{设置其内容1}}显示消息并跳过 TextView设置。如果您没有调用listview,则会移除setMessage(),然后设置TextView

解决方案

因此,要在对话框中显示消息和列表,我建议您使用以下解决方案为AlertDialog设置自定义视图。

listview

这是 final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Dialog title"); final List<String> items = new ArrayList<>(); items.add("Item 1"); items.add("Item 2"); items.add("Item 3"); final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>( getContext(), android.R.layout.simple_list_item_single_choice, items); View content = getLayoutInflater().inflate(R.layout.list_msg_dialog, null); //this is the TextView that displays your message TextView tvMessage = content.findViewById(R.id.tv_msg); tvMessage.setText("Dialog message!!!"); //this is the ListView that lists your items final ListView lvItems = content.findViewById(R.id.lv_items); lvItems.setAdapter(arrayAdapterItems); lvItems.setChoiceMode(ListView.CHOICE_MODE_SINGLE); builder.setView(content); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //do something Toast.makeText(MainActivity.this, items.get(lvItems.getCheckedItemPosition()), Toast.LENGTH_SHORT).show(); } }); final AlertDialog dialog = builder.create(); lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //when you need to act on itemClick } }); dialog.show(); 布局:

list_msg_dialog.xml

答案 2 :(得分:2)

您可以使用以下方法使用列表创建警告对话框:

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");

List<String> items = new ArrayList<String>();   
items.add("RED");
items.add("BLUE");
items.add("GREEN");

final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_expandable_list_item_1, items);

myAlertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});
myAlertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});

myAlertDialog.setAdapter(arrayAdapterItems,
new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        //do what you want
    }
});
myAlertDialog.show();

希望它有所帮助!

答案 3 :(得分:1)

尝试设置alertdialog中的项目,如下所示:

  AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
  myAlertDialog.setTitle("Options");
  myAlertDialog.setMessage("Choose a color.");
  myAlertDialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 CharSequence[] items = {"RED","BLUE","GREEN"};

 myAlertDialog.setItems(items, new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface d, int choice) {

   }
  });
     ............
   myAlertDialog.create();
 myAlertDialog.show();

答案 4 :(得分:1)

这有点棘手,但是您可以使用myAlertDialog.setCustomTitle(myTextView)并自定义yout textView作为具有HTML格式的标题和文本。

TextView textView = new TextView(context);
textView.setText("YourTitle\n\n Your body");
myAlertDialog.setCustomTitle(textView);