在我的应用程序中,我在ListActivity中长按一下上下文菜单。其中一个选项“Priority”会弹出一个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);
}
}
答案 0 :(得分:25)
想出来!我在singleChoiceItem对话框而不是builder.setTitle上使用builder.setMessage。看来使用单选按钮选项的对话框不支持设置消息,只支持标题。虽然提供了该方法,但似乎很奇怪。无论如何,这是工作代码..
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;