Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中添加alertDialog?_Android - Fatal编程技术网

如何在android中添加alertDialog?

如何在android中添加alertDialog?,android,Android,我是android的新手。我想知道如何在wifi设置中添加alertDialog作为确认。alertDialog应包含“是”表示打开wifi,而“否”表示关闭应用程序。 以下是编码的一部分: > // Check for wifi is disabled > if (mainWifi.isWifiEnabled() == false) > { > // If wifi disabled then

我是android的新手。我想知道如何在wifi设置中添加alertDialog作为确认。alertDialog应包含“是”表示打开wifi,而“否”表示关闭应用程序。 以下是编码的一部分:

> // Check for wifi is disabled
>        if (mainWifi.isWifiEnabled() == false)
>             {   
>                 // If wifi disabled then enable it
>                 Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", 
>                 Toast.LENGTH_LONG).show();
>                 
>                 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
> 
>                 // Setting Dialog Title
>                 alertDialog.setTitle("Confirm...");
> 
>                 // Setting Dialog Message
>                 alertDialog.setMessage("Do you want to turn on your wifi?");
> 
> 
>                 /* i want to add in "YES" and "NO" for the wifi setting
>                  * 
>                  * if yes then turn on
>                  * {
>                  * * mainWifi.setWifiEnabled(true);
>                  * }
>                  * if no then close the app
>                  * {
>                  *    // if this button is clicked, close
>               
>                       mainWifi.this.finish();
>                  * }
>                  * 
>                  * 
>                  * 
>                  * */
>                 // Showing Alert Message
>                 alertDialog.show();
>             }

最简单的方法是创建一个
警报对话框生成器
,设置
标题和消息
,并将
是/否
映射到
正/负
按钮,使用
对话框单击侦听器
来处理按钮单击

 AlertDialog.Builder builder = new AlertDialog.Builder(this)
        .setTitle("Dialog Title")
        .setMessage("Dialog Message")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // handle your code to wifi on
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // handle your code to wifi off
            }
        });
    // show the dialog
    builder.show();