5.2 Android Basic QuickStart Widgets&Other View Time Picker

Time Picker

演练时间选择控件。

在本教程中当用户单击按钮后我们将创建TimePickerDialog。从对话框选择的时间将被更新到TextView中。

     

  • 新建项目HelloTimePicker。
  • 打开res/layout/main.xml使用下面的代码:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    >

    <TextView android:id="@+id/timeDisplay"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text=""

    />

    <Button android:id="@+id/pickTime"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Change the time"/>

    </LinearLayout>

         

    3. 打开HelloTimePicker.java 创建成员变量:

    private TextView mTimeDisplay;

    private Button mPickTime;

    private int mHour;

    private int mMinute;

    static final int TIME_DIALOG_ID=0;

         

    4. 修改OnCreate()方法的代码:

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

       

    //capture our view elements

    mTimeDisplay = (TextView)findViewById(R.id.timeDisplay);

    mPickTime = (Button)findViewById(R.id.pickTime);

       

    //add a click listener to THE button

    mPickTime.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

       

    showDialog(TIME_DIALOG_ID);

    }

    });

       

    //get the current time

    final Calendar c = Calendar.getInstance();

    mHour = c.get(Calendar.HOUR_OF_DAY);

    mMinute= c.get(Calendar.MINUTE);

       

    // dispaly the current date

    updateDisplay();

    }

         

    5. 增加updateDisplay()方法和pad()方法:

    //update the time we display in the Textview

    private void updateDisplay(){

    mTimeDisplay.setText(

    new StringBuilder()

    .append(pad(mHour)).append(":")

    .append(pad(mMinute))

    );

    }

       

    private static String pad(int c){

    if(c>=10)

    return String.valueOf(c);

    else

    return "0"+String.valueOf(c);

    }

         

    6. 增加一个TimePickerDialog.OnTimeSetListener的成员,当用户设置新的时间时被调用:

    //the callback received when the user 'sets' the time in the dialog

    private TimePickerDialog.OnTimeSetListener mTimeSetListener=

    new TimePickerDialog.OnTimeSetListener() {

    @Override

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    // TODO Auto-generated method stub

    mHour = hourOfDay;

    mMinute=minute;

    updateDisplay();

    }

    };

    7. 添加onCreateDialog(int)回调方法:

    @Override

    protected Dialog onCreateDialog(int id) {

    // TODO Auto-generated method stub

    switch(id){

    case TIME_DIALOG_ID:

    return new TimePickerDialog(this,

    mTimeSetListener,mHour,mMinute,false);

    }

    return null;

    }

         

    8. 运行应用程序

         

    author: im@xingquan.org

posted @ 2011-03-25 16:24  敏捷学院  阅读(674)  评论(0编辑  收藏  举报