使用自定义DialogFragment作为动态对话框android-java

时间:2019-01-29 02:40:25

标签: java android android-dialogfragment

我创建了一个自定义对话框片段,用作我的项目的YesNo对话框,如下所示

here is the image

我想动态地使用它,不仅可以将它用于一个xml文件。  有人知道如何在通话对话框片段中的appcompact类中设置按钮单击侦听器吗?

我只想在另一个xml文件中设置dialogfragment的buttonclick事件。

我尝试使用接口,但是它给了我一个空指针异常,所以我尝试了下面的代码。

YesNoDialogFragment类

public class DialogYesNo extends DialogFragment {

LayoutInflater inflater;
View v;


@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.dialog_yesno,null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);
    return builder.create();
}
}

DialogFragment的XML文件

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/dialog_background"
android:layout_gravity="center">

<TextView
    android:id="@+id/yesno_title"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="TITLE"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:textSize="10pt"
    android:background="#1b1b01"
    android:textColor="#fff"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="7dp"
    android:paddingLeft="10dp"
    android:textStyle="bold"/>

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:background="?android:attr/listDivider" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#520456">

    <TextView
        android:id="@+id/yesno_message"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Message"
        android:textAllCaps="true"
        android:textSize="12dp"
        android:textColor="#fff"
        android:paddingLeft="10dp"
        android:textStyle="bold"/>

</ScrollView>

<View
    android:id="@+id/divider1"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:background="?android:attr/listDivider" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/dialog_No"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="NO"
        android:background="@drawable/dialog_background"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="10pt"/>
    <Button
        android:id="@+id/dialog_Yes"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="YES"
        android:layout_marginLeft="8dp"
        android:background="@drawable/dialog_background"
        android:textColor="#000"
        android:textStyle="bold"
        android:textSize="10pt"/>
</LinearLayout>

这是MainActivity中的代码

                btnYes = dialogYesNo.getActivity().findViewById(R.id.dialog_Yes);
                btnNo = dialogYesNo.getActivity().findViewById(R.id.dialog_No);

                btnYes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this,TutwithNavigation.class);
                        startActivity(intent);
                    }
                });

                btnNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    // code here

                    }
                });

1 个答案:

答案 0 :(得分:0)

在您的DialogYesNo中尝试findViewById,并为其添加setListener 像这样:

private OnClickListener onYesClick;
private OnClickListener onNoClick;
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    inflater = getActivity().getLayoutInflater();
    v = inflater.inflate(R.layout.dialog_yesno,null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);
    View btnYes = v.findViewById(R.id.dialog_Yes);
    View btnNo = v.findViewById(R.id.dialog_No);               
    btnYes.setOnClickListener(onYesClick);
    btnNo.setOnClickListener(onNoClick);
    return builder.create();
}
}
public void setOnYesClick(OnClickListener listener){
    onYesClick = listener
}
public void setOnNoClick(OnClickListener listener){
    onNoClick = listener
}

改为使用setOnYesClick和setOnNoClick