简体   繁体   English

如何在自定义对话框中创建正面和负面按钮

[英]How can I create positive and negative buttons at custom dialogs

I want to create a custom dialog. 我想创建一个自定义对话框。 So i create a template 'dialog_change' and I open the dialog. 所以我创建了一个模板'dialog_change',然后打开对话框。

Dialog myDialog = new Dialog(Overview.this);
myDialog.setContentView(R.layout.dialog_change);
myDialog.setTitle("My Custom Dialog Title");
myDialog.show();

在此输入图像描述

Now i want to add two button (one positive and one negative button), at the bottom. 现在我想在底部添加两个按钮(一个正面和一个负面按钮)。 How can i do that? 我怎样才能做到这一点?

I'd just make your own custom class to simulate an AlertDialog, this way you can use your own layout with no strings attached. 我只是创建自己的自定义类来模拟AlertDialog,这样您就可以使用自己的布局而不附加任何字符串。 (There are some weird issues where you can't fully get rid of the frame if you want a fully styled AlertDialog). (有一些奇怪的问题,如果你想要一个完全风格的AlertDialog你不能完全摆脱框架)。 Something like this, but you can expand this as fully as you want: 这样的东西,但您可以根据需要完全展开:

public class CustomDialog extends Dialog {
    private Button positive, negative;

    public CustomDialog(Context context) {
        super(context);
        initialize(context);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initialize(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        initialize(context);
    }

    private void initialize(Context c) {
        //Inflate your layout, get a handle for the buttons

        positive = (Button)layout.findViewById(R.id.positive):
        negative = (Button)layout.findViewById(R.id.negative):

        positive.setVisibility(View.GONE);
        negative.setVisibility(View.GONE);
    }

    public void setPositiveButton(String buttonText, View.OnClickListener listener) {
        positive.setText(buttonText);
        positive.setOnClickListener(listener);
        positive.setVisibility(View.VISIBLE);
    }

    public void setNegativeButton(String buttonText, View.OnClickListener listener) {
        negative.setText(buttonText);
        negative.setOnClickListener(listener);
        negative.setVisibility(View.VISIBLE);
    }
}

You can add the two buttons to the custom layout that you are using for dialog(ie dialog_change). 您可以将两个按钮添加到用于对话框的自定义布局(即dialog_change)。 And then you can access them after creating the dialog as follows: 然后您可以在创建对话框后访问它们,如下所示:

Dialog myDialog = new Dialog(Overview.this);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_change,null);
myDialog.setContentView(view);
myDialog.setTitle("My Custom Dialog Title");

Button button1 = (Button)view.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v){
        dialog.dismiss();
    }
});
//Similarly for the second button
myDialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM