gpt4 book ai didi

java - 在 AlertDialogFragment 中通过 Bundle 传递监听器 - 这可能吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:11 25 4
gpt4 key购买 nike

我有一个简单的类:

public class AlertDialogFragment extends DialogFragment {

private static final DialogInterface.OnClickListener DUMMY_ON_BUTTON_CLICKED_LISTENER = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
};

public static final class Builder implements Parcelable {

public static final Creator<Builder> CREATOR = new Creator<Builder>() {
@Override
public Builder createFromParcel(Parcel source) {
return new Builder(source);
}

@Override
public Builder[] newArray(int size) {
return new Builder[size];
}
};

private Optional<Integer> title;
private Optional<Integer> message;
private Optional<Integer> positiveButtonText;
private Optional<Integer> negativeButtonText;

public Builder() {
title = Optional.absent();
message = Optional.absent();
positiveButtonText = Optional.absent();
negativeButtonText = Optional.absent();
}

public Builder(Parcel in) {
title = (Optional<Integer>) in.readSerializable();
message = (Optional<Integer>) in.readSerializable();
positiveButtonText = (Optional<Integer>) in.readSerializable();
negativeButtonText = (Optional<Integer>) in.readSerializable();
}

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeSerializable(title);
out.writeSerializable(message);
out.writeSerializable(positiveButtonText);
out.writeSerializable(negativeButtonText);
}

@Override
public int describeContents() {
return 0;
}

public Builder withTitle(Integer title) {
this.title = Optional.fromNullable(title);
return this;
}

public Builder withMessage(Integer message) {
this.message = Optional.fromNullable(message);
return this;
}

public Builder withPositiveButton(int buttonText) {
this.positiveButtonText = Optional.fromNullable(buttonText);
return this;
}

public Builder withNegativeButton(int buttonText) {
this.negativeButtonText = Optional.fromNullable(buttonText);
return this;
}

private void set(AlertDialog.Builder dialogBuilder, final AlertDialogFragment alertDialogFragment) {
if (title.isPresent()) {
dialogBuilder.setTitle(title.get());
}
if (message.isPresent()) {
dialogBuilder.setMessage(message.get());
}
if (positiveButtonText.isPresent()) {
dialogBuilder.setPositiveButton(positiveButtonText.get(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialogFragment.onPositiveButtonClickedListener.onClick(dialog, which);
}
});
}
if (negativeButtonText.isPresent()) {
dialogBuilder.setNegativeButton(negativeButtonText.get(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialogFragment.onNegativeButtonClickedListener.onClick(dialog, which);
}
});
}
}

public AlertDialogFragment build() {
return AlertDialogFragment.newInstance(this);
}
}


private static final String KEY_BUILDER = "builder";

private DialogInterface.OnClickListener onPositiveButtonClickedListener = DUMMY_ON_BUTTON_CLICKED_LISTENER;
private DialogInterface.OnClickListener onNegativeButtonClickedListener = DUMMY_ON_BUTTON_CLICKED_LISTENER;


private static AlertDialogFragment newInstance(Builder builder) {
Bundle args = new Bundle();
args.putParcelable(KEY_BUILDER, builder);
AlertDialogFragment fragment = new AlertDialogFragment();
fragment.setArguments(args);
return fragment;
}

public void setOnPositiveButtonClickedListener(DialogInterface.OnClickListener listener) {
this.onPositiveButtonClickedListener = listener != null ? listener : DUMMY_ON_BUTTON_CLICKED_LISTENER;
}

public void setOnNegativeButtonClickedListener(DialogInterface.OnClickListener listener) {
this.onNegativeButtonClickedListener = listener != null ? listener : DUMMY_ON_BUTTON_CLICKED_LISTENER;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
Builder builder = getArguments().getParcelable(KEY_BUILDER);
builder.set(alertDialogBuilder, this);
return alertDialogBuilder.create();
}


}

现在我必须直接在 SimpleDialogFragment 中设置按钮点击监听器,因为我无法通过 Bundle 传递监听器 (参数)。但我想 - 所以它看起来像是实例化一个 AlertDialog:

AlertDialogFragment dialogFragment = new AlertDialogFragment.Builder()
.withTitle(R.string.no_internet_connection)
.withMessage(messageId)
.withPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).build();
dialogFragment.show(getSupportFragmentManager(), FRAGMENT_TAG_NO_INTERNET_CONNECTION);

但现在我应该这样设置监听器:

AlertDialogFragment dialogFragment = new AlertDialogFragment.Builder()
.withTitle(R.string.no_internet_connection)
.withMessage(messageId)
.withPositiveButton(android.R.string.ok)
.build();
dialogFragment.setOnPositiveButtonClickListener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialogFragment.show(getSupportFragmentManager(), FRAGMENT_TAG_NO_INTERNET_CONNECTION);

也许将 按钮点击监听器 直接设置到 DialogFragment 实例,而不是通过 Bundle 参数传递它们,是不安全的,因为推荐的将参数传递给 Fragment 的方法是通过 Bundle 参数传递它们。

而且我知道在 Android 中与 Fragment 通信的推荐方法是强制 host activity 实现 回调接口(interface)。但是这种方式并不清楚 Activity 是否应该实现这个接口(interface),直到在运行时抛出 ClassCastException。而且它还具有很强的依赖性——要在 Activity 之外的某个地方使用它,我应该在 Activity 中实现 Callback 接口(interface)。所以我不能在 Fragmenthost Activities 的“独立”中使用它:prepareAlertDialogFragment().show(getActivity().getSupportFragmentManager (), "标签");

最佳答案

听起来您想要一个警报对话框,它可以拥有自己的监听器,可以响应按钮按下事件(有点像 OnClickListener)。我实现这一目标的方法是创建一个自定义 DialogFragment 以及一个扩展 Parcelable 的监听器。

ConfirmOrCancelDialogFragment.java

这是您的对话框实现。它的处理方式与 fragment 非常相似,只是它的实例化方式是通过对 newInstance 的静态方法调用。

public class ConfirmOrCancelDialogFragment extends DialogFragment {
TextView tvDialogHeader,
tvDialogBody;

Button bConfirm,
bCancel;

private ConfirmOrCancelDialogListener mListener;

private String mTitle,
mBody,
mConfirmButton,
mCancelButton;

public ConfirmOrCancelDialogFragment() {
}

public static ConfirmOrCancelDialogFragment newInstance(String title, String body, ConfirmOrCancelDialogListener listener) {
ConfirmOrCancelDialogFragment fragment = new ConfirmOrCancelDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("body", body);
args.putParcelable("listener", listener);
fragment.setArguments(args);
return fragment;
}

public static ConfirmOrCancelDialogFragment newInstance(String title, String body, String confirmButton, String cancelButton, ConfirmOrCancelDialogListener listener) {
ConfirmOrCancelDialogFragment fragment = new ConfirmOrCancelDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("body", body);
args.putString("confirmButton", confirmButton);
args.putString("cancelButton", cancelButton);
args.putParcelable("listener", listener);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_confirm_or_cancel, container);

/* Initial Dialog Setup */
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); // we are using a textview for the title
mListener = getArguments().getParcelable("listener");

/* Link UI */
tvDialogHeader = (TextView) view.findViewById(R.id.tvDialogHeader);
tvDialogBody = (TextView) view.findViewById(R.id.tvDialogBody);
bConfirm = (Button) view.findViewById(R.id.bConfirm);
bCancel = (Button) view.findViewById(R.id.bCancel);

/* Setup UI */
mTitle = getArguments().getString("title", "");
mBody = getArguments().getString("body", "");
mConfirmButton = getArguments().getString("confirmButton", getResources().getString(R.string.yes_delete));
mCancelButton = getArguments().getString("cancelButton", getResources().getString(R.string.no_do_not_delete));

tvDialogHeader.setText(mTitle);
tvDialogBody.setText(mBody);
bConfirm.setText(mConfirmButton);
bCancel.setText(mCancelButton);

bConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onConfirmButtonPressed();
dismiss();
}
});

bCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onCancelButtonPressed();
dismiss();
}
});

return view;
}
}

ConfirmOrCancelDialogListener.java

这是您的监听器实现。您总是可以向其中添加更多内容,但只要确保它扩展了 Parcelable,以便它可以通过 ConfirmOrCancelDialogFragment.java< 中的 newInstance 方法中的包传递.

public interface ConfirmOrCancelDialogListener extends Parcelable {
void onConfirmButtonPressed();

void onCancelButtonPressed();
}

使用示例

这就是事情变得比我想要的更困惑的地方。由于您的监听器正在扩展 Parcelable,因此您还必须重写那些方法,即 describeContentswriteToParcel。幸运的是,它们大部分都是空白的,一切仍然正常。

FragmentManager fm = getActivity().getSupportFragmentManager();
ConfirmOrCancelDialogFragment confirmOrCancelDialogFragment = ConfirmOrCancelDialogFragment.newInstance
(getString(R.string.header), getString(R.string.body),
new ConfirmOrCancelDialogListener() {
@Override
public void onConfirmButtonPressed() {

}

public void onCancelButtonPressed() {
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
);
confirmOrCancelDialogFragment.show(fm, "fragment_delete_confirmation");

这并不能完全回答您通过 AlertDialogFragment 传递它们的问题,但我想如果这个问题这么长时间都没有得到解答,那么值得举一个例子来说明如何使用自定义对话框,它似乎让您可以更好地控制样式和功能。

关于java - 在 AlertDialogFragment 中通过 Bundle 传递监听器 - 这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904955/

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