2011-10-22 103 views

回答

2

请试试这个

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
    switch (which){ 
     case DialogInterface.BUTTON_POSITIVE: 
      //Yes button clicked 

      break; 

     case DialogInterface.BUTTON_NEGATIVE: 
      //No button clicked 
      break; 
    } 
} 
}; 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) 
      .setNegativeButton("No", dialogClickListener).show(); 
+0

喜沙质 感谢您的建议..我需要对话框的结果.. 类似暂停程序执行后dialog.show()和对话框的单击事件后恢复(确定或取消)...! ! – ANKIT

+0

你可以在'//是的按钮点击'时放置代码(当用户点击'yes'时你想要执行的),否则是'//没有按钮的点击' – Sandy

0

为了得到AlertDialog的结果,请如下尝试,

String Result=""; 

public void Alert(String text, String title) 
    { 
     AlertDialog dialog=new AlertDialog.Builder(context).create(); 
     dialog.setTitle(title); 
     dialog.setMessage(text); 
     if(!title.equals("") && !text.equals("")) 
     { 
      dialog.setButton("OK", 
        new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int whichButton) 
         { 
          Result="OK"; 
         } 
        }); 
      dialog.setButton2("Cancel", 
        new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int whichButton) 
         { 
          Result="Cancel"; 
         } 
        }); 
     } 

     dialog.show(); 

    } 

谢谢。

相关问题