Hello All I want to create a dialog from which I can s开发者_如何学Goet date and time together.How can I do this
Thanks in advance
create a dialog with two clickable views to show datePicker and Timepicker .
Create a layout consisting of a DatePicker and a TimePicker in fx a LinearLayout with the orientation set to 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>
Then use this layout to create your dialog.
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
To react to user interacting with your timepicker, do something like this:
TimePicker tp = (TimePicker)dialog.findViewById(R.id.timepicker1);
tp.setOnTimeChangedListener(myOnTimechangedListener);
To get the values from the date- and timepicker when the user has finished setting them, add an OK button in your dialog, and then read the date and time values from the Date- and TimePicker when the user presses OK.
You will probably have to create a simple input box, and then parse the date/time provided. If you give the user a hint as to the formatting that might make your life easier.
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
精彩评论