使用砂浆+流量从对话框中获取用户输入



我用砂浆+流量构建我的应用程序。我试图找出正确的方式来显示一个弹出请求一些文本从用户。我已经创建了这个弹出类:

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# onresolved将用户输入返回给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的文档。我所看到的只是样例项目中的一个基本示例。它们基于confirm对象中的数据创建一个ConfirmerPopup。ConfirmerPopup的目的是根据类声明中给出的Confirmation对象的标题/正文,从用户那里捕获一个布尔值决策。

public class ConfirmerPopup implements Popup<Confirmation, Boolean> {

在您的示例中,您希望捕获来自用户的其他用户输入文本。当popuppresent# 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现在不需要知道任何关于对话框的实现。

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

最新更新