我使用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
的引用。它存储在whatToShow
的PopupPresenter
字段中,但是在调用onPopupResult
之前,此字段已为空。似乎我会不必要地重复自己以保留SavedPage
的其他副本。 最佳答案
PopupPresenter和Popup上没有很多文档。我唯一看到的是示例项目中的一个基本示例。它们基于ConfirmerPopup对象中的数据创建Confirmation。 ConfirmerPopup的目的是根据类声明所看到的给予Confirmation对象的标题/正文来捕获用户的布尔决策。
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());
}
}
}