无法解雇自定义的DialogFragment Android

时间:2015-07-22 12:32:56

标签: 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;

    }
}

1 个答案:

答案 0 :(得分:1)

直接在Button Click Listener

中设置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();
        }
    });
    .......
 }