gpt4 book ai didi

java - 实现 DialogFragment 的回调

转载 作者:行者123 更新时间:2023-12-01 16:50:43 24 4
gpt4 key购买 nike

从我的 Recylerview Adapter 中,每个项目都有多个按钮,我想要的是在单击时将 DialogFragment 显示为确认,并在单击 PositiveButton 时执行不同的方法/操作。

问题是我不知道如何在单击 positiveButton 时实现回调,并且可以根据要单击的按钮执行不同的方法或操作每个项目。

例如在我的适配器类上

@Override
public void onBindViewHolder(final PageOnlineAdapter.TheViewHolder holder, final int position) {

holder.btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);

holder.btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);

holder.btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);

holder.btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
}

这是我的 DialogFragment 类

public class ConfirmationDialog extends DialogFragment {

public static ConfirmationDialog newInstance(String title, String description) {
ConfirmationDialog frag = new ConfirmationDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("description", description);
frag.setArguments(args);
return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String description = getArguments().getString("description");
return new AlertDialog.Builder(getActivity())
//.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setMessage(description)
.setPositiveButton("CONTINUE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//How to tell when this positiveButton is clicked
}
}
)
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//How to tell when this negativeButton is clicked
}
}
)
.create();
}
}

最佳答案

首先,我想说感谢Merve Sahin创建Interface的想法。

我最终使用了下面的代码,而不是接受的答案。这使我拥有一类 DialogFragment 并将其用于许多情况例如 通过多个按钮调用并根据单击的按钮进行响应

首先,您需要导入以下内容:

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

对于AlertDialog,创建一个类:

public class ConfirmDialog extends DialogFragment {

public static final String TITLE = "title", CONTENT = "content";
private OnContinueCancelClick continueCancelClick;

public static ConfirmDialog newInstance(String title, String content) {
ConfirmDialog frag = new ConfirmDialog();
Bundle args = new Bundle();
args.putString(TITLE, title);
args.putString(CONTENT, content);
frag.setArguments(args);
return frag;
}

public void setOnContinueCancelClick(OnContinueCancelClick continueCancelClick) {
this.continueCancelClick = continueCancelClick;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString(TITLE);
String content = getArguments().getString(CONTENT);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title).setMessage(content)
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(continueCancelClick != null)
continueCancelClick.onCancelClicked();
}
})
.setPositiveButton("CONTINUE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(continueCancelClick != null)
continueCancelClick.onContinueClicked();
}
});
Dialog dialog = builder.create();
return dialog;

}

//Declare an Interface
public interface OnContinueCancelClick{
void onContinueClicked();
void onCancelClicked();
}
}

当显示对话框并且您需要单击按钮时,请说一下我的情况:

holder.btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/** Start the Dialog Here **/

ConfirmDialog confirmDialog = ConfirmDialog.newInstance("Titile of Dialog","Content or Description of Dialog"); //Setup Dialog
confirmDialog.show(((AppCompatActivity)contextView).getSupportFragmentManager(), "confirmDialog"); //Show the Dialog
confirmDialog.setOnContinueCancelClick(new ConfirmDialog.OnContinueCancelClick(){ //listen on Clicked Button
@Override
public void onContinueClicked() {
//Do some operation or call some functions when Continue button is Clicked
}
@Override
public void onCancelClicked() {
//Do Nothing or Do some operation or call some functions when Cancel button is Clicked
}
});

/** END **/
}
);

您可以使用自定义 View 布局执行相同的操作

public class ConfirmDialog extends DialogFragment {

private Button btnCancel, btnContinue;

public static final String TITLE = "title", CONTENT = "content";
private OnContinueCancelClick continueCancelClick;

public static ConfirmDialog newInstance(String title, String content) {
ConfirmDialog frag = new ConfirmDialog();
Bundle args = new Bundle();
args.putString(TITLE, title);
args.putString(CONTENT, content);
frag.setArguments(args);
return frag;
}

public void setOnContinueCancelClick(OnContinueCancelClick continueCancelClick) {
this.continueCancelClick = continueCancelClick;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_custom_layout, container);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

//Some initialization or condition here...
btnCancel = (Button)view.findViewById(R.id.btn_cancel); //Intialize your Cancel Button
btnContinue = (Button)view.findViewById(R.id.btn_continue); //Intialize your Continue Button

getDialog().setTitle(getArguments().getString(TITLE)); //Set the Title

btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(continueCancelClick != null)
continueCancelClick.onCancelClicked(); //Return this
dismiss(); //Close the Dialog when Done
}
});

btnContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(continueCancelClick != null)
continueCancelClick.onContinueClicked(); //Return this
dismiss(); //Close the Dialog when Done
}
});
}

public interface OnContinueCancelClick{
void onContinueClicked();
void onCancelClicked();
}
}

这可以帮助其他人,因此我在使用界面的帮助下发布了我的解决方案。

这个Question回答者 Nepster也为我完成这个任务提供了很多帮助。

希望这对你们有帮助。

关于java - 实现 DialogFragment 的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40656451/

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