TimePicker时间选择器和DatePicker日期选择器

时间:2022-04-05 18:08:29

①Layout的xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >


    <TimePicker
        android:id="@+id/myTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <DatePicker
        android:id="@+id/myDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

②MainActivity.class的编程代码

public class MainActivity extends ActionBarActivity {


private TimePicker myTime;
private DatePicker myDate;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTime = (TimePicker) findViewById(R.id.myTime);
myDate = (DatePicker) findViewById(R.id.myDate);


// 显示当前时间
myTime.setIs24HourView(true);
myTime.setCurrentHour(18);
myTime.setCurrentMinute(30);


// 显示当前年月
myDate.updateDate(myDate.getYear(), myDate.getMonth(), myDate.getDayOfMonth());
}
}TimePicker时间选择器和DatePicker日期选择器