Hello All我想创建一个对话框,我可以从中设置日期和时间。我该怎么做
提前致谢
答案 0 :(得分:2)
创建一个包含两个可点击视图的对话框,以显示datePicker和Timepicker。
答案 1 :(得分:1)
在fx和LinearLayout中创建一个由DatePicker和TimePicker组成的布局,其方向设置为vertical。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<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>
</LinearLayout>
然后使用此布局创建对话框。
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
要对与您的时间选择器进行交互的用户做出反应,请执行以下操作:
TimePicker tp = (TimePicker)dialog.findViewById(R.id.timepicker1);
tp.setOnTimeChangedListener(myOnTimechangedListener);
要在用户完成设置后从日期和时间选择器获取值,请在对话框中添加“确定”按钮,然后在用户按“确定”时从Date-和TimePicker中读取日期和时间值。 / p>
答案 2 :(得分:0)
您可能需要创建一个简单的输入框,然后解析提供的日期/时间。如果您向用户提供有关可能使您的生活更轻松的格式的提示。
Toast(...."Please enter the date/time (mm/dd/yyyy hh:mm:ss)"...).show();
String datetime = findViewById(R.id.datetime_input);
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
Date date = formatter.parse(datetime);
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html