创建包含TimePicker和DatePicker的Dialog中的问题?

时间:2011-08-09 05:55:17

标签: 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"
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" />

我的用于创建自定义对话框的Java代码是

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

        }
    });

    DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1);




    Button btn_ok = (Button) dialog.findViewById(R.id.buttondatetime);
    btn_ok.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            String text = "Time "+hours+":"+minute;
            tv_date_time.setText(text);
            dialog.cancel();
        }
    });

}

@覆盖     protected Dialog onCreateDialog(int id)     {         // TODO自动生成的方法存根

    switch (id) 
    {
    case DIALOG_1:
        dialogDateTime();
        break;
    case DIALOG_2:
        dialogTwo();
        break;
    default:
        break;
    }
    return super.onCreateDialog(id);
}

我没有像TimePicker那样获得DatePicker的任何方法所以我无法使用DatePicker 请帮忙

1 个答案:

答案 0 :(得分:5)

感谢您不回复我自己找到答案.. 我改变了我的方法

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

    }

    );

}

感谢链接Android Date and Time Controls