带列表选择项的警报框仍为空

时间:2021-04-14 18:54:40

I would like to check a number of Spinner controls in the Onclick event of a few buttons (if the user moves away from the Activity), and if one or more were not filled out, display a warning message with 2 options to the user:

我想在几个按钮的Onclick事件中检查一些Spinner控件(如果用户离开Activity),如果一个或多个未填写,则向用户显示一条带有2个选项的警告消息:

@Override
public void onClick(View v) {

    int i1 = spinner1.getSelectedItemPosition();
        (..)

    if ((i1 == 0) | (i2 == 0) | (i3 == 0) | (i4 == 0) | (i5 == 0)) {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("Answers missing, what do you want to do?");
        final CharSequence[] items = {
                "Oops.. let me fix that…",
                "I want to leave the app now" };
        alertbox.setTitle("Pick an item");
        alertbox.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 1) {
                                        finish();
                                      // should lead back to the spinner controls on the page

                }else if (item == 2){
                    finish();
                android.os.Process.killProcess(android.os.Process
                            .myPid());
                }
            }
        }).create();
        alertbox.show();

However, I just see an empty box with the title, but the message set with setMessage() is not displayed, and I don't get any list items for selection - the box is empty! Any idea what I can do to fix that?

但是,我只看到一个带有标题的空框,但是没有显示使用setMessage()设置的消息,并且我没有得到任何列表项供选择 - 框是空的!知道我能做些什么来解决这个问题吗?

1 个解决方案

#1


9  

I have been having the same issue, and I found that if I remove the .setMessage line, then the list appears with the title. Not sure why this is though. If you use the .setpositive button, .negative, etc, then you can have a message. I also noticed that google examples all don't use .setMessage with an item list. They only use .setTitle, which doesn't give you that much space.
So, while this doesn't help you if you want a long message, it will get your list visible if that is your main goal.

我一直有同样的问题,我发现如果我删除.setMessage行,那么列表会显示标题。不知道为什么会这样。如果你使用.setpositive按钮,.negative等,那么你可以有一条消息。我还注意到谷歌示例都不使用带有项目列表的.setMessage。他们只使用.setTitle,它不会给你那么多空间。所以,虽然如果您想要一条长信息,这对您没有帮助,但如果这是您的主要目标,它将使您的列表可见。

#1


9  

I have been having the same issue, and I found that if I remove the .setMessage line, then the list appears with the title. Not sure why this is though. If you use the .setpositive button, .negative, etc, then you can have a message. I also noticed that google examples all don't use .setMessage with an item list. They only use .setTitle, which doesn't give you that much space.
So, while this doesn't help you if you want a long message, it will get your list visible if that is your main goal.

我一直有同样的问题,我发现如果我删除.setMessage行,那么列表会显示标题。不知道为什么会这样。如果你使用.setpositive按钮,.negative等,那么你可以有一条消息。我还注意到谷歌示例都不使用带有项目列表的.setMessage。他们只使用.setTitle,它不会给你那么多空间。所以,虽然如果您想要一条长信息,这对您没有帮助,但如果这是您的主要目标,它将使您的列表可见。