简体   繁体   English

如何在自定义对话框中设置自定义按钮?

[英]How to set custom button in custom dialog box?

My dialog box is a custom layout dialogbox_solution with one title and two buttons. 我的对话框是一个自定义布局dialogbox_solution,带有一个标题和两个按钮。 Here is XML file with only main lines : 这是只包含主线的XML文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout>

    <TextView
        android:id="@+id/txtDiaMsg"
        />

    <TableLayout
        android:layout_below="@+id/txtDiaMsg">
        <TableRow>
            <Button
                android:id="@+id/yesButton"
                android:text=" YES "/>
            <Button
                android:id="@+id/noButton"
                android:text=" NO "/>
        </TableRow>
    </TableLayout>
</RelativeLayout>

How can i link my custom positive button and negative button programmatically ? 如何以编程方式链接我的自定义正面按钮和负面按钮?

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which){
                                    case DialogInterface.BUTTON_POSITIVE:
                                        //Yes button clicked
                                       // do something here
                                        break;

                                    case DialogInterface.BUTTON_NEGATIVE:
                                        //No button clicked
                                        break;
                                }
                            }
                        };
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        LayoutInflater li = getLayoutInflater();
                        View view = li.inflate(R.layout.dialogbox_solution, null);
                        builder.setView(view);
                        builder.show();

Create somthing like this... First create your layout xml file... for eg: dialog.xml... and then call it like the below code wherever you want... 创建这样的东西...首先创建你的布局xml文件...例如:dialog.xml ...然后像下面的代码一样调用它,无论你想要什么...

 final Dialog myDialog = new Dialog(this);
 myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        myDialog.setContentView(R.layout.dialog);
        myDialog.setCancelable(false);
        Button yes = (Button) myDialog.findViewById(R.id.share);
        Button no = (Button) myDialog.findViewById(R.id.no);
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do your code here
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //Do your code here
            }
        });

        myDialog.show();
        myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.cancel();
                    return true;
                }
                return false;
            }
        });

dialog.xml dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#e7e7e9">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/title"
    android:id="@+id/textView10"
    android:textSize="20sp"
    android:textColor="#000000"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />


<Button
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:text="@string/yes"
    android:id="@+id/share"
    android:background="@drawable/loginbutton"
    android:textSize="12sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_gravity="left|bottom"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/textView15"
    android:layout_alignLeft="@+id/textView15"
    android:layout_alignStart="@+id/textView15" />

<Button
    android:layout_width="120dp"
    android:layout_height="40dp"
    android:text="@string/no"
    android:id="@+id/no"
    android:layout_gravity="bottom|right"
    android:background="@drawable/loginbutton"
    android:textSize="12sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_alignTop="@+id/share"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

  </RelativeLayout>

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

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