Android AlertDialog单键

Ele*_*ec0 159 android builder button android-alertdialog

我想要一个AlertDialog构建器,它只有一个按钮,表示OK或Done或者其他东西,而不是默认的yes和no.可以使用标准的AlertDialog完成,还是我必须使用其他东西?

Wil*_*ate 365

难道只能通过使用正面按钮来完成吗?

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();
Run Code Online (Sandbox Code Playgroud)

  • @ScottBiggs 对话框中的按钮对齐方式由系统决定,可以在设备、版本和语言(RTL 等)之间改变。应用程序不应尝试设置对齐方式,但应尝试设置意图(正面、负面等) (2认同)

muz*_*uzz 45

你可以用这个:

AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setTitle("Title");
builder1.setMessage("my message");
builder1.setCancelable(true);
builder1.setNeutralButton(android.R.string.ok,
        new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert11 = builder1.create();
alert11.show();
Run Code Online (Sandbox Code Playgroud)


Xar*_*mer 11

另一种方法

Builder alert = new AlertDialog.Builder(ActivityName.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show(); 
Run Code Online (Sandbox Code Playgroud)

奖金

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
builder.setMessage("Message dialog with three buttons");

       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });

      builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });

       builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener()     {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)

现在由您决定使用一个,两个或三个按钮..


Fac*_*ano 8

如果Android API是智能的,那么我就越接近一个班轮这应该是:

new AlertDialog.Builder(this)
    .setMessage(msg)
    .setPositiveButton("OK", null)
    .show();
Run Code Online (Sandbox Code Playgroud)