android - 无法关闭自定义 DialogFragment Android

标签 android onclick android-dialogfragment dismiss dialogfragment

我使用自定义布局实现了自己的 DialogFragment,其中有两个简单的按钮。问题是我无法驳回它。这是布局的代码:

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:l

ayout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imgProfileQuestionSender"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/questionToAnswer"
        android:layout_gravity="center_horizontal"
        android:padding="10dp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/yes"
            android:id="@+id/yesButtonAnswer"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no"
            android:id="@+id/noButtonAnswer"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

这里是我的Dialog的代码

public class QuestionDialog extends DialogFragment implements View.OnClickListener {
 public interface QuestionInterface{
    void yesQuestionPressed(int idQuestion);
    void noQuestionPressed(int idQuestion);
}

private int idQuestion;

private QuestionInterface mQuestionInterface;
//private DialogInterface.OnClickListener mOnclickListener;
private Button yesButton, noButton;
private ImageView profileImage;
private TextView question;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.layout_dialog_question, null);
    noButton = (Button)view.findViewById(R.id.noButtonAnswer);
    yesButton = (Button)view.findViewById(R.id.yesButtonAnswer);
    profileImage = (ImageView)view.findViewById(R.id.imgProfileQuestionSender);
    question = (TextView)view.findViewById(R.id.questionToAnswer);
    question.setText("here there should be the text of the question retrived using the id of the question, also the image on the left should be the image of" +
            "the friend that sent the question, the id of the question is " + String.valueOf(getArguments().getInt("questionId")));
    Picasso.with(getActivity().getApplicationContext()).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(profileImage);


    setCancelable(false);
    return view;

}


@Override
public void onClick(View v) {
    if(v.getId()==R.id.yesButtonAnswer){

        dismiss();
        mQuestionInterface.yesQuestionPressed(getArguments().getInt("questionId"));

    }
    if(v.getId()==R.id.noButtonAnswer){

        dismiss();
        mQuestionInterface.noQuestionPressed(getArguments().getInt("questionId"));

    }
}


@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    if(activity instanceof QuestionInterface) {
        mQuestionInterface = (QuestionInterface) activity;

    }
}

最佳答案

直接在onCreateView(...)中设置您的按钮点击监听器

 @Override
 public View onCreateView(....)
 {
 .......
 .......

 negativeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });

    positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    .......
 }

关于android - 无法关闭自定义 DialogFragment Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31563290/

相关文章:

java - Git 是离开 WIP 分支并开始在另一个分支上工作的更好方法

Javascript onclick 事件不适用于变量引用

javascript - &lt;input&gt; 按钮未作为按钮运行,找不到如何运行 js 文件

android - FLAG_SECURE 不适用于样式为 DialogFragment.STYLE_NO_TITLE 的 DialogFragment

android - 如何通过单击首选项启动 DialogFragment?

javascript - 在 Android 7 上使用 cordova-plugin-file 不起作用

java - 如何获取 Google 返回的 JSON 对象中的持续时间和距离?

c++ - 检测何时单击按钮? [C++, WinAPI]

Android:FragmentTransaction 隐藏不适用于 DialogFragment

android - 如何在android中创建水平菜单?