2 znl 12 znl_12 于 2013.07.04 09:56 提问

关于 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
才显示。如何实现?

3个回答

LiDaming_and
LiDaming_and   2013.07.04 15:03
已采纳

调用 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();
    }
Jave_
Jave_   2013.07.04 10:55

试试这个

if(list.size == 0)
  ListDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.GONE);  
else
  ListDialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);  
mianbaocheng
mianbaocheng   2013.07.23 16:46

if(list.size!= 0){

ListDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do something
}
});
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!