首先请耐心等待我,因为我是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"
}
}
感谢您的帮助。谢谢。
答案 0 :(得分: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.html。 http://developer.android.com/guide/topics/ui/dialogs.html
答案 1 :(得分: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();
}
}
答案 2 :(得分: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();
}
}
答案 3 :(得分: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();
}