Android AlertDialog单按钮

问题描述:

我想要一个AlertDialog构建器,只有一个按钮,说OK或完成或某事,而不是默认的是和否。 这可以用标准的AlertDialog完成,还是必须使用别的东西?Android AlertDialog单按钮

难道这只是通过只使用一个积极的按钮?

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(); 
+0

尼斯简单解决方案 – 2015-11-04 20:56:22

+1

工程很棒,只要您不介意单个按钮右对齐。 :( – 2015-12-16 21:23:46

+0

@ScottBiggs对话框中的按钮对齐由系统决定,可能会在设备,版本和语言(RTL等)之间切换。应用程序不应尝试设置对齐方式,但意图(正面,负面等) – aberaud 2016-08-24 21:35:09

您可以使用此:

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(); 

在单为Android,你可以这样做:

var ad = new AlertDialog.Builder(this); 
ad.SetTitle("Title"); 
ad.SetMessage("Message"); 
ad.SetPositiveButton("OK", delegate { ad.Dispose(); }); 
ad.Show(); 

代码重用,你可以把它在这样

的方法
public static Dialog getDialog(Context context,String title, String message, DialogType typeButtons) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setTitle(title) 
     .setMessage(message) 
       .setCancelable(false); 

     if (typeButtons == DialogType.SINGLE_BUTTON) { 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //do things 
        } 
       }); 
     } 

     AlertDialog alert = builder.create(); 

     return alert; 
    } 

    public enum DialogType { 
     SINGLE_BUTTON 

    } 

//其他代码重用问题,如usin用于提供反馈的g接口也非常出色。

+1

我打电话给这太复杂了,而不是代码重用。这太复杂了,不能维护... – 2014-10-16 08:15:03

+0

@MarianPaździoch你为什么这么想? – MSaudi 2015-04-09 07:21:08

+0

如果我想添加四个按钮怎么办? – 2017-04-10 06:02:41

另一种方法

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

奖金

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(); 

现在它是由你使用一个,两个或三个按钮..

这我离得越近d获取到一个班轮这应该是如果Android的API是任何智能:

new AlertDialog.Builder(this) 
    .setMessage(msg) 
    .setPositiveButton("OK", null) 
    .show(); 
+0

这不会导致一个按钮是正确的吗? – 2017-08-03 17:10:17

它非常简单

new AlertDialog.Builder(this).setView(input).setPositiveButton("ENTER",    
         new DialogInterface.OnClickListener()      
         { public void onClick(DialogInterface di,int id)  
          { 
           output.setText(input.getText().toString()); 
          } 
         } 
        ) 
     .create().show(); 

如果您想阅读完整的节目在这里看到: Program to take input from user using dialog and output to screen