4

我是 Java/Android 开发的新手(我昨晚开始学习),所以我完全有可能在做一些非常愚蠢的事情。然而,经过一个多小时的谷歌搜索,我什么也没想到。我使用 Eclipse 作为我的编辑器。

我正在阅读此处的文档AlertDialog,其中给出了一个示例:

public static class MyAlertDialogFragment extends DialogFragment {

    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((FragmentAlertDialog)getActivity()).doNegativeClick();
                        }
                    }
                )
                .create();
    }
}

我最初重写了它,所以我可以开始将一些方法提交到内存中,但是得到一个错误"FragmentAlertDialog cannot be resolve to a type"。我点击Ctrl++ShiftO确保我有正确的导入,但它仍然没有消失。

所以我复制/粘贴了示例代码并按以下顺序执行了以下操作:

  1. 点击Ctrl++以获得正确的导入(使用, not ShiftOandroid.app.DialogFragmentandroid.support.v4.app.DialogFragment
  2. 在顶部声明我的包裹
  3. 分别替换为R.string.alert_dialog_ok和_R.string.alert_dialog_cancelandroid.R.string.okandroid.R.string.cancel
  4. 已删除setIcon(),因为我还没有要放入的图标

我仍然收到错误:

  1. FragmentAlertDialog无法解析为类型 (x4)
  2. 类的非法修饰符MyAlertDialogFragment;只有public, abstract&final是允许的

我做错了什么,还是示例代码有问题?

4

2 回答 2

1

1.FragmentAlertDialog

确保您要投射到的 Activity 被命名为FragmentAlertDialog。确保还保存所有内容 - 有时 Eclipse 在保存所有内容之前不会建立连接。

2.MyAlertDialogFragment 类的非法修饰符;只允许公开、抽象和最终

取出static修饰符:

public class MyAlertDialogFragment extends DialogFragment {

或保留static并移动此 Fragment,使其包含在您想要的 Activity 中。这意味着它MyAlertDialogFragment应该在您的 Activity 中,该 Activity 的右括号之前。

我是 Java/Android 开发的新手

不要从这么复杂的事情开始。学习Java,然后转向Android。

于 2013-03-02T17:17:48.430 回答
1

嗨,试试这些代码来实现警报对话框

       AlertDialog.Builder alert2 = new AlertDialog.Builder(this);  
            alert2.setTitle("Your Title");
            alert2.setMessage("Your Messages");
            final EditText input2 = new EditText(this);
            input2.setInputType(InputType.TYPE_CLASS_PHONE);
            alert2.setView(input2);
            alert2.setPositiveButton(GButton, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Do something with value!                 
                    try
                    {
                    // do your stuff here   
                    }
                    catch(Exception e)
                    {

                    }
                } 
            });

            alert2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });

            alert2.show();
于 2013-03-02T17:21:37.757 回答