DialogFragment 无法使用 1 个按钮。

发布于 2024-12-09 15:21:23 字数 1196 浏览 0 评论 0原文

为什么我的代码不起作用?

我需要带有 1 个按钮的dialogFragment。


我有 2 个课程:

   public class MyAlertDialogFragment extends DialogFragment {

 static DialogFragment newInstance(int num) {
        MyAlertDialogFragment f = new MyAlertDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }


 public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.alertdialog, container, false);
     Button button = (Button)v.findViewById(R.id.button1);

     return v;
 }

}

和活动:

   public class DialogFragmentActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
showDialog();
}

void showDialog() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
    newFragment.show(ft, "dialog");
}

    }   

你如何完成这个片段?

Why doesn't my code work?

I need dialogFragment with 1 button.


I have 2 classes:

   public class MyAlertDialogFragment extends DialogFragment {

 static DialogFragment newInstance(int num) {
        MyAlertDialogFragment f = new MyAlertDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }


 public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.alertdialog, container, false);
     Button button = (Button)v.findViewById(R.id.button1);

     return v;
 }

}

and Activity:

   public class DialogFragmentActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
showDialog();
}

void showDialog() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
    newFragment.show(ft, "dialog");
}

    }   

How do you complete this fragment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2024-12-16 15:21:23

不要重写 onCreateView,重写 onCreateDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity())
            .setPositiveButton(android.R.string.ok, null);
    return alert.create();
}

Don't override onCreateView, override onCreateDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity())
            .setPositiveButton(android.R.string.ok, null);
    return alert.create();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文