问题描述
- 关于 AlertDialog PositiveButton 和 NegativeButton 的问题
-
我使用下面的代码在AlertDialog中显示文件夹列表:ListDialog = new AlertDialog.Builder(MyActivity.this); ListDialog.setTitle("Folder List"); ListView folder = new ListView(MyActivity.this); //scan folder folder.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { //scan sub-folder }); ListDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { //do something } }); ListDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { //back } });
我想让 AlertDialog的 NegativeButton 总是显示,而当 list item 数是0时, AlertDialog的 PositiveButton
才显示。如何实现?
解决方案
调用 Dialog 方法:
dialog_message("Dialog msg");
public void dialog_message(String msg)
{
final AlertDialog alertDialog = new AlertDialog.Builder(BottomMenu_Event.this).create();
alertDialog.setTitle("Title");
alertDialog.setIcon(R.drawable.icon);
alertDialog.setMessage(msg);
alertDialog.setButton("Save", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
//Your Code....
}
});
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
alertDialog.dismiss();
}
});
alertDialog.show();
}
解决方案二:
试试这个
if(list.size == 0)
ListDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.GONE);
else
ListDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
解决方案三:
if(list.size!= 0){
ListDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do something
}
});
}
时间: 2025-01-19 18:00:29