【问题标题】:Android custom dialog instantiateAndroid 自定义对话框实例化
【发布时间】:2013-12-01 14:02:03
【问题描述】:

我正在努力学习Dialogs

根据 androids 官方文档的教程,我创建了自己的自定义对话框:

public class LoginDialog extends DialogFragment {

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.login, null))
           // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                // sign in the user ...
              }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 LoginDialog.this.getDialog().cancel();
               }
            });
    return builder.create();
  }
}

现在我想从我的活动中调用它。

所以我做了以下事情:(from my Login activity)

FragmentTransaction ft = getFragmentManager().beginTransaction();
android.app.Fragment prev = getFragmentManager().findFragmentByTag("dialog");
DialogFragment fragment = new LoginDialog(prev);

但是我得到以下错误:

prev cannot be applied to android.app.fragment

我不确定我做错了什么或我应该从这里去哪里。

【问题讨论】:

标签: android dialog fragment


【解决方案1】:

得到:

prev 不能应用于 android.app.fragment

因为 LoginDialog 类不包含任何以 Fragment 作为参数并返回 LoginDialog 实例的构造函数。如果您使用的是 Android DialogFragment Example,您会忘记返回 LoginDialog DialogFragment 实例的newInstance 方法。你应该以同样的方式做或尝试:

FragmentManager fm = getFragmentManager();
LoginDialog fragment = new LoginDialog();
fragment.show(fm, "dialog");

【讨论】:

  • 这在 fragment.show(fm, "dialog"); 处给了我以下错误;无法解析方法 show(android.app.FragmentManger, java.lang.string)
  • @MarcRasmussen : 见这里 [developer.android.com/reference/android/app/… DialogFragment 中提供的两种方法都尝试以相同的方式进行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
相关资源
最近更新 更多