创建同时包含 TimePicker 和 DatePicker 的对话框时出现问题?

发布于 2024-11-28 11:32:43 字数 2226 浏览 2 评论 0原文

我正在创建一个自定义对话框,我想在其中使用 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();
        }
    });

}

@Override 受保护的对话框 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 请帮忙

I am creating a Custom dialog where I want to use both DatePicker and TimePicker .Although I am able to use TimePicker as I want but for DatePicker I am short of ideas.
Please Help Me.
My Code is as follows

My XML Layout file is...

<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" />

And My Java Code for Creating Custom Dialog is

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

}

@Override
protected Dialog onCreateDialog(int id)
{
// TODO Auto-generated method stub

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

I am not getting any method for DatePicker as in TimePicker So I am not able to use DatePicker
Please Help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-12-05 11:32:43

感谢您没有回复我自己找到了答案..
我这样改变了我的方法

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和时间控制

Thanks for not replying I found answer on my own ..
I changed my method like this

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

    }

    );

}

Thanks to link Android Date and Time Controls

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文