Android 向对话框添加正按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10710888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:42:19  来源:igfitidea点击:

Add positive button to Dialog

androidandroid-layout

提问by JoeyCK

I have a very simple custom dialog and I wan't to add a positive button without having to modify the XML file, just like you would do it with an AlertDialog but I don't know if it's possible. This is the code:

我有一个非常简单的自定义对话框,我不想在不必修改 XML 文件的情况下添加肯定按钮,就像您使用 AlertDialog 做的那样,但我不知道是否可能。这是代码:

final Dialog dialog = new Dialog(MyActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Settings");
dialog.show();

回答by amp

You should use the builder.

您应该使用构建器。

LayoutInflater inflater = LayoutInflater.from(this);
View dialog_layout = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog_root_layout));
AlertDialog.Builder db = new AlertDialog.Builder(MyActivity.this);
db.setView(dialog_layout);
db.setTitle("settings");
db.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    }
});
AlertDialog dialog = db.show();

回答by MattDavis

You can use the AlertDialog.Builder class:

您可以使用 AlertDialog.Builder 类:

http://developer.android.com/reference/android/app/AlertDialog.Builder.html

http://developer.android.com/reference/android/app/AlertDialog.Builder.html

Create a new instance of it with AlertDialog.Builder myAlertDialogBuilder = new AlertDialog.Builder(context). Then use methods such as setTitle()and setView()to customize it. This class also has methods for setting the buttons. setPositiveButton(String, DialogInterface.OnClickListener)to set up your buttons. Finally, use AlertDialog myAlertDialog = myAlertDialogBuilder.create()to get your instance of AlertDialog, which you can then further customize with methods such as setCancelable().

使用AlertDialog.Builder myAlertDialogBuilder = new AlertDialog.Builder(context). 然后使用诸如setTitle()和 之类的方法对其setView()进行自定义。这个类也有设置按钮的方法。 setPositiveButton(String, DialogInterface.OnClickListener)设置您的按钮。最后,使用AlertDialog myAlertDialog = myAlertDialogBuilder.create()获取您的 AlertDialog 实例,然后您可以使用诸如setCancelable().

Edit: Also, from the docs: http://developer.android.com/guide/topics/ui/dialogs.html

编辑:另外,来自文档:http: //developer.android.com/guide/topics/ui/dialogs.html

"The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly. Instead, you should use one of the... subclasses"

“Dialog 类是创建对话框的基类。但是,您通常不应该直接实例化 Dialog。相反,您应该使用...子类之一”

If you really don't want to use an AlertDialog, it'll probably be best to extend the Dialog class yourself, rather than using it as-is.

如果您真的不想使用 AlertDialog,最好自己扩展 Dialog 类,而不是按原样使用它。

回答by Atiar Talukdar

You also can use this function

您也可以使用此功能

public void showMessage(String title,String message)
{
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setPositiveButton("OK", new
            DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    builder.show();
}