Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建同时包含时间选择器和日期选择器的对话框时出现问题?_Android_Datepicker_Timepicker - Fatal编程技术网

Android 创建同时包含时间选择器和日期选择器的对话框时出现问题?

Android 创建同时包含时间选择器和日期选择器的对话框时出现问题?,android,datepicker,timepicker,Android,Datepicker,Timepicker,我正在创建一个自定义对话框,我想在其中同时使用DatePicker和TimePicker。虽然我可以使用TimePicker,但对于DatePicker,我缺乏想法。 请帮帮我。 我的代码如下 我的XML布局文件是 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

我正在创建一个自定义对话框,我想在其中同时使用DatePicker和TimePicker。虽然我可以使用TimePicker,但对于DatePicker,我缺乏想法。 请帮帮我。 我的代码如下

我的XML布局文件是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TimePicker android:id="@+id/timePicker1"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker>
<DatePicker android:id="@+id/datePicker1"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker>
<Button android:id="@+id/buttondatetime" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="OK" />
@凌驾 受保护的对话框onCreateDialogint id { //TODO自动生成的方法存根

    switch (id) 
    {
    case DIALOG_1:
        dialogDateTime();
        break;
    case DIALOG_2:
        dialogTwo();
        break;
    default:
        break;
    }
    return super.onCreateDialog(id);
}
我没有像TimePicker中那样获得DatePicker的任何方法,因此我无法使用DatePicker
请帮忙

谢谢你没有回复,我自己找到了答案。。 我这样改变了我的方法

void dialogDateTime()
{
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.datetime);
    dialog.setCancelable(true);
    dialog.setTitle("This is Dialog 1");
    dialog.show();

    TimePicker time_picker = (TimePicker) dialog.findViewById(R.id.timePicker1);
    time_picker.setOnTimeChangedListener(new OnTimeChangedListener()
    {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minutes)
        {
            // TODO Auto-generated method stub
            // Toast.makeText(CustomDialog.this,
            // "hourOfDay = "+hourOfDay+"minute = "+minute , 1000).show();
            hours = hourOfDay;
            minute = minutes;
        }
    });

    final DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1);
    Button btn = (Button) dialog.findViewById(R.id.buttondatetime);
    btn.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub

            TextView txt = (TextView) findViewById(R.id.textView1);
            txt.setText("You selected " + date_picker.getDayOfMonth() + "/" + (date_picker.getMonth() + 1) + "/"
                    + date_picker.getYear());
            txt.append("Time "+ hours + ":" +minute);

            dialog.cancel();
        }

    }

    );

}

感谢链接

谢谢你没有回复,我自己找到了答案。。 我这样改变了我的方法

void dialogDateTime()
{
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.datetime);
    dialog.setCancelable(true);
    dialog.setTitle("This is Dialog 1");
    dialog.show();

    TimePicker time_picker = (TimePicker) dialog.findViewById(R.id.timePicker1);
    time_picker.setOnTimeChangedListener(new OnTimeChangedListener()
    {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minutes)
        {
            // TODO Auto-generated method stub
            // Toast.makeText(CustomDialog.this,
            // "hourOfDay = "+hourOfDay+"minute = "+minute , 1000).show();
            hours = hourOfDay;
            minute = minutes;
        }
    });

    final DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1);
    Button btn = (Button) dialog.findViewById(R.id.buttondatetime);
    btn.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub

            TextView txt = (TextView) findViewById(R.id.textView1);
            txt.setText("You selected " + date_picker.getDayOfMonth() + "/" + (date_picker.getMonth() + 1) + "/"
                    + date_picker.getYear());
            txt.append("Time "+ hours + ":" +minute);

            dialog.cancel();
        }

    }

    );

}

多亏了link

,问题在于如何使用在XML版面中创建的DatePicker视图。问题在于如何使用在XML版面中创建的DatePicker视图。+1很高兴您找到了解决方案并将其发布到此处以供参考。+1很高兴您找到了解决方案并将其发布到此处以供参考。