【问题标题】:Android AlertDialog not showing radio buttons or messageAndroid AlertDialog 不显示单选按钮或消息
【发布时间】:2011-03-02 19:07:31
【问题描述】:

在我的应用程序中,我在 ListActivity 中长按时会弹出一个上下文菜单。 “优先级”选项之一会弹出一个带有 3 个单选按钮选项的 AlertDialog。问题是,它显示了一个空对话框,没有我的 3 个选项或我设置的消息。这是我的代码..

protected Dialog onCreateDialog(int id) {
    AlertDialog dialog;
    switch(id) {
    case DIALOG_SAB_PRIORITY_ID:
        final CharSequence[] items = {"High", "Normal", "Low"};

        AlertDialog.Builder builder = new AlertDialog.Builder(SabMgmt.this);
        builder.setMessage("Select new priority")
               .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });

        dialog = builder.create();            
        break;
    default:
        dialog = null;
    }
    return dialog;
}

如果我将 .setSingleChoiceItems 替换为正负按钮,它会按预期显示按钮和消息。我在设置单选按钮列表时做错了什么?这也是我的调用代码..

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case R.id.sabdelete:
        // Correct position (-1) for 1 header
        final SabQueueItem qItem = (SabQueueItem) itla.getItem(info.position-1);
        SabNZBdUtils.deleteItem(qItem.getNzo_id());
        getQueue();
        ListView lv = getListView();
        View v = lv.findViewById(R.id.sablistheader);
        setHeader(v);
        itla.notifyDataSetChanged();
        return true;
    case R.id.sabpriority:
        showDialog(DIALOG_SAB_PRIORITY_ID);
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

【问题讨论】:

    标签: android radio-button android-alertdialog


    【解决方案1】:

    想通了!我在 singleChoiceItem 对话框上使用 builder.setMessage 而不是 builder.setTitle。似乎使用单选按钮选项的对话框不支持设置消息,只支持标题。虽然提供了该方法似乎很奇怪。无论如何,这是工作代码..

    protected Dialog onCreateDialog(int id) {
        AlertDialog dialog;
        switch(id) {
        case DIALOG_SAB_PRIORITY_ID:
            final CharSequence[] items = {"High", "Normal", "Low"};
    
            AlertDialog.Builder builder = new AlertDialog.Builder(SabMgmt.this);
            builder.setTitle("Select new priority")
                   .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                }
            });
    
            dialog = builder.create();  
            break;
        default:
            dialog = null;
        }
        return dialog;
    

    【讨论】:

    • 谢谢!我只是用头撞墙了一个小时,试图弄清楚为什么我的警报对话框一直空着。
    • 非常感谢。这太疯狂了。这可以记录在某处。
    • 坦克!我疯狂地寻找这个错误!
    • 这不是一个错误,而是一个功能;)我同意我理解为什么不这样做并不是很好
    • 非常感谢您的帮助,感谢您分享您的知识。你怎么猜到的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2019-07-02
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多