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

时间:2015-10-06 10:17:07

标签: android dialog datetimepicker

我是一个新的Android并且在如何在单个date picker中添加这两个time pickerdialog box并努力指导我如何才能创建{{1}并使用函数dialog box来膨胀它,但我得到了异常

setview

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

            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();

        }
    });

enter image description here

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

<?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$1.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)

2 个答案:

答案 0 :(得分:1)

您应该使用R.layout.linearLayout1,而不是R.id.linearLayout1,如下所示:

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

假设您的布局文件的名称为linearLayout1

答案 1 :(得分:1)

我已经在github上为此创建了一个项目。我采用了与您不同的方法,并通过扩展对话框类创建了带日期和时间选择器的自定义对话框。

https://github.com/ramesh130/DateTimePickerDialog