android setNegativeButton到自定义对话框

问题描述 投票:2回答:2

我有自己的自定义对话框,如下所示:

public class BerekenDialog extends Dialog{
    public BerekenDialog(Context context) {
        super(context);
        setContentView(R.layout.bereken_dialog_layout);

        this.setTitle("Bereken");
        //dostuff.
    }
}

我用这个开始我的对话:

BerekenDialog bd = new BerekenDialog (this);
bd.show();

有没有办法将alertdialog中的负,正和中性按钮添加到我的自定义对话框中?

android android-alertdialog android-dialog
2个回答
2
投票
get data from some spinners, make a calculation with the data and display it

您可以在不创建自定义对话框的情况下执行此操作,只有在构建器中创建正面和负面按钮时才会执行正面和负面按钮。没有办法实现它,而是可以使用按钮在xml布局中创建自己的。

在不使用自定义的情况下创建alertdialog:

    AlertDialog a = new AlertDialog.Builder(this)
    .setTitle("Bereken")
    .setView(getLayoutInflater().inflate( R.layout.bereken_dialog_layout,null))
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // ok button
        }
     })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // cancel button
        }
     }).create();
    TextView tv = (TextView)a.findViewById(r.id.your_id); //use the instance of textview from layout of dialog
    tv.setText("update"); //set is before displaying
    a.show();

0
投票

您可以添加2个按钮,使它们看起来像AlertDialog.Builder中的按钮。

你创建了2个按钮,你只需要改变它们的背景颜色并在它们周围添加一些边框。这可以通过创建drawable(按钮边框形状)来完成:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <stroke android:width="1dp" android:color="#bdbdbd" />
    <solid android:color="#f2f2f2" />
</shape>

然后,您需要将此drawable作为背景颜色应用于自定义对话框xml中的按钮:

android:background="@drawable/button_border_shape"
© www.soinside.com 2019 - 2024. All rights reserved.