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