当前位置:操作系统 > 安卓/Android >>

Android 程式开发:(十一)选择控件 —— 11.2 DatePicker

DataPicker和上一节讲的TimePicker很相似。使用DatePicker,可以让用户选择一个特定的日期。下面展示如何使用DatePicker。

1、使用上一节的工程,BasicViews4,修改main.xml。

[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
<Button android:id="@+id/btnSet" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="I am all set!" 
    android:onClick="onClick" /> 
 
<DatePicker android:id="@+id/datePicker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
 
<TimePicker android:id="@+id/timePicker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
 
</LinearLayout> 
2、F11调试。竖屏模式显式DatePicker稍微有一些窄,按Ctrl+F11,可以更改屏幕的显示方向。

\
3、在BasicViews4Activity.java中添加一些代码

[java] 
package net.learn2develop.BasicViews4; 
 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.TimePicker; 
import android.widget.Toast; 
 
public class BasicViews4Activity extends Activity { 
    TimePicker timePicker; 
    DatePicker datePicker; 
 
    int hour, minute; 
    int yr, month, day; 
 
    static final int TIME_DIALOG_ID = 0; 
    static final int DATE_DIALOG_ID = 1; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        timePicker = (TimePicker) findViewById(R.id.timePicker); 
        timePicker.setIs24HourView(true); 
 
        // showDialog(TIME_DIALOG_ID); 
        datePicker = (DatePicker) findViewById(R.id.datePicker); 
 
        //---get the current date--- 
        Calendar today = Calendar.getInstance(); 
        yr = today.get(Calendar.YEAR); 
        month = today.get(Calendar.MONTH); 
        day = today.get(Calendar.DAY_OF_MONTH); 
 
        showDialog(DATE_DIALOG_ID);         
    } 
 
    @Override 
    protected Dialog onCreateDialog(int id) 
    { 
        switch (id) { 
        case TIME_DIALOG_ID: 
            return new TimePickerDialog( 
                    this, mTimeSetListener, hour, minute, false); 
        case DATE_DIALOG_ID: 
            return new DatePickerDialog( 
                    this, mDateSetListener, yr, month, day); 
 
        } 
        return null; 
    } 
 
    private DatePickerDialog.OnDateSetListener mDateSetListener = 
            new DatePickerDialog.OnDateSetListener() 
    { 
        public void onDateSet( 
                DatePicker view, int year, int monthOfYear, int dayOfMonth) 
        { 
            yr = year; 
            month = monthOfYear; 
            day = dayOfMonth; 
            Toast.makeText(getBaseContext(), 
                    "You have selected : " + (month + 1) + 
                    "/" + day + "/" + year, 
                    Toast.LENGTH_SHORT).show(); 
        } 
    }; 
 
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = 
            new TimePickerDialog.OnTimeSetListener() 
    { 
        public void onTimeSet( 
                TimePicker view, int hourOfDay, int minuteOfHour) 
        { 
            hour = hourOfDay; 
            minute = minuteOfHour; 
 
            SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");          
            Date date = new Date(0,0,0, hour, minute);&n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,