0

首先请多多包涵,因为我是 Android 新手。

我想写一个应用程序,如果满足条件,可以弹出一个对话框

例子 :

公共类TestMax {

       public void main(String[] args) {
          int i = 5;
          int j = 5;
          int sum = i + j;

                   if (sum == 10) {


                // alert dialog box will appear and show the message -  "Answer is 10"

               }


    }

感谢你的帮助。谢谢你。

4

4 回答 4

1

满足您的条件时添加:

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int which) {
  // TODO Add your code for the button here.   }
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();

请参阅http://developer.android.com/reference/android/app/AlertDialog.htmlhttp://developer.android.com/guide/topics/ui/dialogs.html

于 2012-11-09T06:56:15.473 回答
0

做这个。如果您只想显示一个警报对话框。

public void main(String[] args) 
{
        int i = 5;
        int j = 5;
        int sum = i + j;

        if (sum == 10) {
            new AlertDialog.Builder(yourclass.this)
                    .setTitle("Your answer is")
                    .setMessage(i)
                    .setNeutralButton("ok", null)
                    .setIcon(android.R.drawable.stat_sys_warning).show();
        }
} 
于 2012-11-09T06:58:43.700 回答
0

请尝试此代码....

弹出对话框的类

if (condition) {

                showAlertDialog(Activityname.this, "Internet Connection",
                        "You have internet connection", true);
            } else {

                showAlertDialog(Activityname.this, "No Internet Connection",
                        "You don't have internet connection.", false);
            }

showdailog 的方法声明

    public void showAlertDialog(Context context, String title, String message, Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
    alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

}

于 2012-11-09T06:59:34.730 回答
0
public void main(String[] args) {
          int i = 5;
          int j = 5;
          int sum = i + j;

                   if (sum == 10) {
                   showAlertDialog(Activityname.this, "Internet Connection",
                    "You have internet connection", true);

                // alert dialog box will appear and show the message -  "Answer is 10"

               }

       public void showAlertDialog(Context context, String title, String message,   Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

     // Setting Dialog Title
      alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
    alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

    // Setting OK Button
   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       }
     });

   // Showing Alert Message
    alertDialog.show();


     }
于 2012-11-09T07:02:26.443 回答