【问题标题】:Can't dismiss a custom DialogFragment Android无法关闭自定义 DialogFragment Android
【发布时间】:2015-07-22 12:32:56
【问题描述】:

我使用自定义布局实现了我自己的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;

    }
}

【问题讨论】:

    标签: android onclick android-dialogfragment dismiss dialogfragment


    【解决方案1】:

    直接在onCreateView(...)中设置你的Button Click Listener

     @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();
            }
        });
        .......
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 2020-10-30
      • 2014-10-23
      • 2011-12-24
      • 2013-04-27
      • 1970-01-01
      相关资源
      最近更新 更多