Android 如果满足某些条件,则显示AlertDialog
首先请容忍我,因为我是Android新手 我想写一个应用程序,可以弹出一个对话框,如果条件得到满足 例如: 公共类TestMax{Android 如果满足某些条件,则显示AlertDialog,android,android-alertdialog,Android,Android Alertdialog,首先请容忍我,因为我是Android新手 我想写一个应用程序,可以弹出一个对话框,如果条件得到满足 例如: 公共类TestMax{ public void main(String[] args) { int i = 5; int j = 5; int sum = i + j; if (sum == 10) { // alert dialog box
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"
}
}
感谢您的帮助。谢谢。在满足您的条件时添加以下内容:
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();
参考。
.如果只想显示警报对话框,请执行此操作
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();
}
}
请尝试此代码 类以弹出对话框
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();
}
}@user1811408如果您对我的答案感到满意,请单击我答案左侧的“单击检查”按钮。
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();
}