首先,请忍受我,因为我是新手。
我想编写一个可以弹出对话框的应用程序
示例:
公共类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"
}
}
感谢您的帮助。谢谢。
在满足您的病情时添加它:
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。
请尝试此代码....
弹出对话框的课程
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();
}
}
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();
}
执行此操作。如果您只想显示警报对话框。
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();
}
}