gpt4 book ai didi

android - 无法关闭自定义 DialogFragment Android

转载 作者:行者123 更新时间:2023-11-30 02:00:29 26 4
gpt4 key购买 nike

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com