这是我的警报代码,它会在按下按钮时显示一条消息。我如何制作一个OK按钮以解散?
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setCancelMessage(null);
alert.show();
}
});
}}
我已经设置了alert.setCancelMessage(null),但它没有显示任何要关闭的按钮。
感谢所有答案。
解决方案:
Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK", null);
alert.show();
}
});
答案 0 :(得分:44)
将alertdialog更改为:
Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();
答案 1 :(得分:11)
您可以使用此功能为对话框创建“确定”和“取消”按钮,
AlertDialog.Builder builder = new AlertDialog.Builder(
getApplicationContext());
builder.setCancelable(true);
builder.setTitle("Title");
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
答案 2 :(得分:3)
难道只能使用正面按钮来完成吗?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
答案 3 :(得分:1)
您可以使用setPositiveButton
和setNegativeButton
alert.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface alert, int which) {
// TODO Auto-generated method stub
//Do something
alert.dismiss();
}
});
alert.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface alert, int which) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
编辑:
alert.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// Do something
alert.dismiss();
}
});
alert.setButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
答案 4 :(得分:0)
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ab = new AlertDialog.Builder(MainActivity.this);
ab.setTitle("confirm");
ab.setIcon(R.drawable.ic_launcher);
ab.setMessage("Do u want to close this application?");
ab.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
}
});
ab.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
Toast.makeText(MainActivity.this, "cancle", Toast.LENGTH_SHORT).show();
}
});
ab.show();
}
});