的Android AlertDialog没有显示单选按钮或消息
-
26-09-2019 - |
题
在我的应用我带来了上长按一个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);
}
}
解决方案
想通了!我用一个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;
不隶属于 StackOverflow