DatePicker and Timepicker are two easy-to-use controls that are derived from Framelayout, where DatePicker is for the user to select a date and timepicker for the user to select the time
DatePicker and Timepicker on the basis of Framelayout provides a number of methods to obtain the current user selected date, time, if the program needs to get the user selected date, time, You can listen by adding ondatechangedlistener to the DatePicker, adding ontimerchangedlistener for Timepicker
The following is a demonstration of the use of DatePicker and timepicker using an example that allows the user to select a date and time.
Example: User Select Date, time
In order for the user to select a date, the application needs to use both DatePicker and timepicker two components, and to bind listeners to them, the following is the interface layout of the application
Layout of 1.DatePicker Components
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout
xmlns:android= "http://schemas.android.com/apk/ Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o rientation= "vertical" >
<textview
android:layout_width= "fill_parent"
android:layout_height= " Wrap_content "
android:text=" Select the time to buy this book "/>
<datepicker
android:id=" @+id/datapicker
" Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= " Center_horizontal "/>
<edittext
android:id=" @+id/show "android:layout_width=" Fill_parent "
android:layout_height= "wrap_content"
android:editable= "false"
android:cursorvisible= "false"
android:background= "#B7B7B7"/>
</LinearLayout>
Bind a listener for DatePicker
Mainactivity.java
package com.example.date_time;
import java.text.ChoiceFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
private int year;
private int month;
private int day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
DatePicker datapicker = (DatePicker)findViewById(R.id.datapicker);
//Get the current year, month and day
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
//Initialize the DatePicker component, specify the listener during initialization
datapicker.init(year, month, day, new OnDateChangedListener() {
To
@Override
public void onDateChanged(DatePicker arg0, int year, int month, int day) {
// TODO Auto-generated method stu
MainActivity.this.year = year;
MainActivity.this.month = month;
MainActivity.this.day = day;
//Display the current date
showDate(year, month, day);
To
}
});
}
private void showDate(int year,int month,int day) {
EditText show = (EditText)findViewById(R.id.show);
show.setText("The date is"+year+"year"+month+"month"+day+"day");
}
}
Here is the result of the run
Well, Timepicker is the same reason, we can try.