无法关闭自定义 DialogFragment Android
Can't dismiss a custom DialogFragment Android
我用我的自定义布局实现了我自己的 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(...)
中设置你的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();
}
});
.......
}
我用我的自定义布局实现了我自己的 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(...)
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();
}
});
.......
}