Android AlertDialog 单按钮

Posted

技术标签:

【中文标题】Android AlertDialog 单按钮【英文标题】:Android AlertDialog Single Button 【发布时间】:2011-08-14 04:23:56 【问题描述】:

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

【问题讨论】:

【参考方案1】:

难道不能只使用一个肯定按钮来完成吗?

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

【讨论】:

效果很好,只要您不介意那个右对齐的单个按钮。 :( @ScottBiggs 对话框中的按钮对齐方式由系统决定,可以在设备、版本和语言(RTL 等)之间更改。应用程序不应尝试设置对齐方式,而是设置意图(正面、负面等) 太棒了!谢谢! 我不希望按钮执行任何操作,它应该只是关闭警报框,创建一个空的 onClick 侦听器是一个好习惯还是应该使用不同的方法。【参考方案2】:

你可以用这个:

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

【讨论】:

【参考方案3】:

另一种方法

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

现在由你决定使用一个、两个或三个按钮..

【讨论】:

【参考方案4】:

如果 Android API 很聪明的话,我可以更接近这一行:

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

【讨论】:

那不会导致一个按钮右对齐吗? 简单易行。谢谢【参考方案5】:

为了代码重用,你可以用这样的方法来实现

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

    

//其他代码重用问题,例如使用接口提供反馈也将非常好。

【讨论】:

@MarianPaździoch 你为什么这么认为? 如果我想添加四个按钮怎么办? 如果它只是一个没有监听器的带有单个按钮的“确认对话框”,这没关系。除此之外,不值得包裹在这样的助手中,因为您需要处理每个按钮的按下。【参考方案6】:

科特林?

  val dialogBuilder = AlertDialog.Builder(this.context)
  dialogBuilder.setTitle("Alert")
               .setMessage(message)
               .setPositiveButton("OK", null)
               .create()
               .show()

【讨论】:

【参考方案7】:

警报对话框

带有单个按钮的警报对话框。

带有图标的警报对话框。

带有三个按钮的警报对话框。

带有选择选项的警报对话框,单选按钮。

带有多选选项的警报对话框,复选框按钮。

<resources>
    <string name="app_name">Alert Dialog</string>

    <string name="info_dialog">Info Dialog</string>
    <string name="icon_dialog">Icon Dialog</string>
    <string name="rate_dialog">Rate Dialog</string>
    <string name="singleOption_dialog">Single Options Dialog</string>
    <string name="multiOption_dialog">Multi Options Dialog</string>

    <string-array name="fruit_name">
        <item>Apple</item>
        <item>Banana</item>
        <item>Orange</item>
        <item>Grapes</item>
        <item>Watermelon</item>
        <item>Nothing</item>
    </string-array>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/info_dialog"
        android:layout_
        android:layout_
        android:text="@string/info_dialog"
        android:textAllCaps="false"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp" />

    <Button
        android:id="@+id/icon_dialog"
        android:layout_
        android:layout_
        android:text="@string/icon_dialog"
        android:textAllCaps="false"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp" />

    <Button
        android:id="@+id/rate_dialog"
        android:layout_
        android:layout_
        android:text="@string/rate_dialog"
        android:textAllCaps="false"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp" />

    <Button
        android:id="@+id/single_dialog"
        android:layout_
        android:layout_
        android:text="@string/singleOption_dialog"
        android:textAllCaps="false"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp" />

    <Button
        android:id="@+id/multi_dialog"
        android:layout_
        android:layout_
        android:text="@string/multiOption_dialog"
        android:textAllCaps="false"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14sp" />
</LinearLayout>

public class MainActivity extends AppCompatActivity 

    String select;
    String[] fruitNames;
    Button infoDialog, iconDialog, rateDialog, singleOptionDialog, multiOptionDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        infoDialog = findViewById(R.id.info_dialog);
        rateDialog = findViewById(R.id.rate_dialog);
        iconDialog = findViewById(R.id.icon_dialog);
        singleOptionDialog = findViewById(R.id.single_dialog);
        multiOptionDialog = findViewById(R.id.multi_dialog);
        infoDialog.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                infoDialog();
            
        );
        rateDialog.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                ratingDialog();
            
        );
        iconDialog.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                iconDialog();
            
        );
        singleOptionDialog.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                SingleSelectionDialog();
            
        );
        multiOptionDialog.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                MultipleSelectionDialog();
            
        );
    

    /*Display information dialog*/
    private void infoDialog() 
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Info");
        dialogBuilder.setMessage("Some informative message for the user to do that.");
        dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.dismiss();
            
        );
        dialogBuilder.create().show();
    

    /*Display rating dialog*/
    private void ratingDialog() 
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Rate Us");
        dialogBuilder.setMessage("If you liked it, please rate it. It will help us grow.");
        dialogBuilder.setPositiveButton("Rate", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.dismiss();
            
        );
        dialogBuilder.setNegativeButton("Leave it", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.dismiss();
            
        );
        dialogBuilder.setNeutralButton("May be, later", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.dismiss();
            
        );
        dialogBuilder.create().show();
    

    /*Dialog with icons*/
    private void iconDialog() 
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Info");
        dialogBuilder.setIcon(R.mipmap.ic_launcher_round);
        dialogBuilder.setMessage("You know, you could have provided some valuable message here!");
        dialogBuilder.setPositiveButton("Got it", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.dismiss();
            
        );
        dialogBuilder.create().show();
    

    /*Dialog to select single option*/
    private void SingleSelectionDialog() 
        fruitNames = getResources().getStringArray(R.array.fruit_name);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
        dialogBuilder.setTitle("Which fruit you want to eat?");
        dialogBuilder.setSingleChoiceItems(fruitNames, -1, new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialogInterface, int i) 
                //Toast.makeText(MainActivity.this, checkedItem, Toast.LENGTH_SHORT).show();
                select = fruitNames[i];
            
        );
        dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                Toast.makeText(MainActivity.this, "Item selected: " + select, Toast.LENGTH_SHORT).show();
            
        );
        dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
            
        );
        dialogBuilder.create().show();
    

    /*Dialog to select multiple options*/
    public void MultipleSelectionDialog() 
        final String[] items = "Apple", "Banana", "Orange", "Grapes", "Watermelon";
        final ArrayList<Integer> selectedList = new ArrayList<>();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choice multi item fruit list");
        builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) 
                if (isChecked) 
                    selectedList.add(which);
                 else if (selectedList.contains(which)) 
                    selectedList.remove(which);
                
            
        );
        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialogInterface, int i) 
                ArrayList<String> selectedStrings = new ArrayList<>();
                for (int j = 0; j < selectedList.size(); j++) 
                    selectedStrings.add(items[selectedList.get(j)]);
                
                Toast.makeText(getApplicationContext(), "Items selected: " + Arrays.toString(selectedStrings.toArray()), Toast.LENGTH_SHORT).show();
            
        );
        builder.show();
    

【讨论】:

【参考方案8】:

很简单

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

【讨论】:

【参考方案9】:

在 Mono for Android 中,您可以这样做:

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

【讨论】:

以上是关于Android AlertDialog 单按钮的主要内容,如果未能解决你的问题,请参考以下文章

关闭 AlertDialog 后获取单选按钮状态

Android AlertDialog笔记

Android AlertDialog笔记

AlertDialog(警告对话框)

Android:如何设置AlertDialog的宽度和高度,以及AlertDialog风格的按钮?

Android Studio基础使用单选对话框AlertDialog