如何将日期和时间选择器放入 android 的对话框中

how can i put date and time picker in a dialog in android

我是 android 中的新手,我正在努力解决如何将这两个 date pickertime picker 添加到一个 dialog box 中,请指导我如何做我创建了一个 dialog box 并使用函数 setview 对其进行充气,但出现异常

            AlertDialog.Builder dialog = new AlertDialog.Builder(
                    MainActivity.this);
            dialog.setTitle("Begning");
            dialog.setPositiveButton("ok",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        }
                    });
            dialog.setNegativeButton("cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });
             View pickers=getLayoutInflater().inflate(R.id.linearLayout1,
             null);
            dialog.setView(picker);

            dialog.show();

        }
    });

以上是我的代码 听到的是 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<DatePicker
     android:id="@+id/datePicker1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >
</DatePicker>

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TimePicker>

我想创建这样的东西请帮助我

    10-06 15:49:24.261: E/Trace(9287): error opening trace file: No such file or directory (2)
10-06 15:49:27.434: W/dalvikvm(9287): threadid=1: thread exiting with uncaught exception (group=0x411903a8)
10-06 15:49:27.454: E/AndroidRuntime(9287): FATAL EXCEPTION: main
10-06 15:49:27.454: E/AndroidRuntime(9287): android.content.res.Resources$NotFoundException: Resource ID #0x7f080000 type #0x12 is not valid
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2150)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.content.res.Resources.getLayout(Resources.java:860)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at com.example.datetimedialog.MainActivity.onClick(MainActivity.java:51)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.View.performClick(View.java:4102)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.view.View$PerformClick.run(View.java:17085)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.os.Handler.handleCallback(Handler.java:615)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.os.Looper.loop(Looper.java:155)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at android.app.ActivityThread.main(ActivityThread.java:5520)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at java.lang.reflect.Method.invoke(Method.java:511)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
10-06 15:49:27.454: E/AndroidRuntime(9287):     at dalvik.system.NativeStart.main(Native Method)

这是我在 运行

时得到的 logcat

你应该使用 R.layout.linearLayout1,而不是 R.id.linearLayout1,像这样:

View pickers=getLayoutInflater().inflate(R.layout.linearLayout1,null);

假设您的布局文件的名称是 linearLayout1.

我为此在 github 上创建了一个项目。我采用了与您不同的方法,并通过扩展对话框 class.

创建了带有日期和时间选择器的自定义对话框

https://github.com/ramesh130/DateTimePickerDialog