簡體   English   中英

如何設置自定義對話框內的onclicklistener按鈕

[英]How to set onclicklistener button which is inside a custom dialog

我是android的新手,在調用對話框內按鈕的onclicklistener時,我得到空指針異常。 我該怎么辦。

btn_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog d=new Dialog(MainActivity.this);
                d.setContentView(R.layout.activity_main);
                d.setTitle("Add content");
                d.show();
                btnsubmit = (Button) findViewById(R.id.btn_submit);
                btnsubmit.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        String name = etName.getText().toString();
                        String phoneNo = etPhone.getText().toString();

                        String query = "INSERT INTO PHONE_CONTACTS(name,phone) values ('"
                                + name + "','" + phoneNo + "')";
                        sqlHandler.executeQuery(query);
                        showList();
                        etName.setText("");
                        etPhone.setText("");

                    }
                });
            }
        });

我將非常感謝你的幫助。 這是我的對話框xml的樣子: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <TableLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >



        <TableRow

                android:id="@+id/tableRow1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" >



            <TextView

                    android:id="@+id/textView1"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="@string/name" />



            <EditText

                    android:id="@+id/et_name"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:ems="10" >

            </EditText>

        </TableRow>



        <TableRow

                android:id="@+id/tableRow2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content" >



            <TextView

                    android:id="@+id/textView1"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:text="@string/phone" />



            <EditText

                    android:id="@+id/et_phone"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:ems="10" >

            </EditText>

        </TableRow>

        <Button
                android:id="@+id/btn_submit"
                android:layout_width="80dp"
                android:layout_height="40dp"
                android:text="@string/submit"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"/>
    </TableLayout>

</RelativeLayout>

您需要使用對話框對象來初始化視圖

   btnsubmit = (Button) d.findViewById(R.id.btn_submit);

此外,如果您在自定義對話框中有edittext,則可以以相同的方式初始化它們。

   EditText etName = (EdiText) d.findViewById(R.id.et_Name);
   EditText etPhone = (EdiText) d.findViewById(R.id.et_Phoe);

您可以通過調用dialog.dismiss()來關閉對話框

       final Dialog d=new Dialog(MainActivity.this);
            d.show();
            btnsubmit = (Button) d.findViewById(R.id.btn_submit);
            btnsubmit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    d.dismiss();
                }
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM