gpt4 book ai didi

java - 使用 Mortar + Flow 从对话框中获取用户输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:04 26 4
gpt4 key购买 nike

我正在使用 Mortar 构建我的应用程序+ Flow .我试图找出正确的方法来显示一个弹出窗口,该弹出窗口请求用户提供一些文本。我创建了这个弹出类:

public class SavedPageTitleInputPopup implements Popup<SavedPageTitleInput, Optional<String>> {

private final Context context;

private AlertDialog dialog;

public SavedPageTitleInputPopup(Context context) {
this.context = context;
}

@Override public Context getContext() {
return context;
}

@Override
public void show(final SavedPageTitleInput info, boolean withFlourish,
final PopupPresenter<SavedPageTitleInput, Optional<String>> presenter) {
if (dialog != null) throw new IllegalStateException("Already showing, can't show " + info);

final EditText input = new EditText(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setText(info.savedPage.getName());

dialog = new AlertDialog.Builder(context).setTitle(info.title)
.setView(input)
.setMessage(info.body)
.setPositiveButton(info.confirm, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface d, int which) {
dialog = null;
final String newTitle = Strings.emptyToNull(String.valueOf(input.getText()));
presenter.onDismissed(Optional.fromNullable(newTitle));
}
})
.setNegativeButton(info.cancel, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface d, int which) {
dialog = null;
presenter.onDismissed(Optional.<String>absent());
}
})
.setCancelable(true)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override public void onCancel(DialogInterface d) {
dialog = null;
presenter.onDismissed(Optional.<String>absent());
}
})
.show();
}

@Override public boolean isShowing() {
return dialog != null;
}

@Override public void dismiss(boolean withFlourish) {
dialog.dismiss();
dialog = null;
}
}

这个类按预期工作。它使用 SavedPage 确定要在对话框中显示的内容,并使用 PopupPresenter#onDismissed 将用户输入返回给 PopupPresenter当按下正确的按钮时。

我的问题是编写用于显示对话框和处理输入的 PopupPresenter 子类。这是我现在拥有的:

new PopupPresenter<SavedPage, Optional<String>>() {
@Override protected void onPopupResult(Optional<String> result) {
if (result.isPresent()) {
// The user entered something, so update the API
// Oh wait, I don't have a reference to the SavedPage
// that was displayed in the dialog!
}
}
}

正如评论所说,我没有对对话框中显示的 SavedPage 的引用。它存储在 PopupPresenterwhatToShow 字段中,但在调用 onPopupResult 之前该字段被清空。似乎我会不必要地重复自己以保留 SavedPage 的额外副本。

最佳答案

关于 PopupPresenter 的文档还不多和 Popup .我唯一看到的是示例项目中的一个基本示例。他们创建了一个 ConfirmerPopup基于 Confirmation 内的数据目的。 ConfirmerPopup 的目的是根据类声明中提供给 Confirmation 对象的标题/正文来捕获用户的 boolean 决定。

public class ConfirmerPopup implements Popup<Confirmation, Boolean> {

在您的情况下,您希望从用户那里捕获额外的用户输入文本。当调用 PopupPresenter#onPopupResult 时,结果对象应包含 SavedPageTitleInputPopup 所需的所有数据。如下修改您的 SavedPageTitleInputPopup

public class SavedPageTitleInputPopup implements Popup<SavedPage, SavedPageResults> {
private final Context context;
private AlertDialog dialog;

public SavedPageTitleInputPopup(Context context) {
this.context = context;
}

@Override public Context getContext() {
return context;
}

@Override
public void show(SavedPage info, boolean withFlourish, final PopupPresenter<SavedPage, SavedPageResults> presenter) {
if (dialog != null) throw new IllegalStateException("Already showing, can't show " + info);
// Create your Dialog but scrape all user data within OnClickListeners
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
//Anything else you need to do... .setView() or .setTitle() for example
builder.setPositiveButton(info.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
dialog = null;
//Save data to SavedPageResults
final SavedPageResults results = new SavedPageResults():
presenter.onDismissed(results);
}
});
builder.setNegativeButton(info.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
dialog = null;
final SavedPageResults results = new SavedPageResults();
presenter.onDismissed(results);
}
});
dialog = builder.show();
}

@Override public boolean isShowing() {
return dialog != null;
}

@Override public void dismiss(boolean withFlourish) {
dialog.dismiss();
dialog = null;
}
}

您的 PopupPresenter 现在不需要了解有关 Dialog 实现的任何信息。

new PopupPresenter<SavedPage, SavedPageResults>() {
@Override protected void onPopupResult(SavedPageResults result) {
if (result.isPresent()) {
updateUi(result.getSavedText());
}
}
}

关于java - 使用 Mortar + Flow 从对话框中获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23426034/

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