基本视图——TimePicker

作者:追风剑情 发布于:2015-8-31 21:46 分类:Android

示例一: 在活动中显示TimePicker

视图


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7.  
  8. <TimePicker android:id="@+id/timePicker"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. <Button android:id="@+id/btnSet"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="I am all set!"
  15. android:onClick="onClick" />
  16.  
  17. </LinearLayout>


代码


  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TimePicker;
  7. import android.widget.Toast;
  8.  
  9. public class BasicViewsActivity extends Activity {
  10.  
  11. TimePicker timePicker;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_basic_views);
  16. timePicker = (TimePicker) findViewById(R.id.timePicker);
  17. timePicker.setIs24HourView(true);
  18. }
  19. public void onClick(View view) {
  20. Toast.makeText(getBaseContext(), "Time selected:" +
  21. timePicker.getCurrentHour() +//getCurrentHour()总是返回24小时格式
  22. ":"+timePicker.getCurrentMinute(),
  23. Toast.LENGTH_SHORT).show();
  24. }
  25. }


运行效果

ttttttttttttttttttt.png

示例二: 在一个对话框窗口中显示TimePicker

代码

  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TimePicker;
  7. import android.widget.Toast;
  8. import android.app.Dialog;
  9. import android.app.TimePickerDialog;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12.  
  13. public class BasicViewsActivity extends Activity {
  14.  
  15. TimePicker timePicker;
  16. int hour, minute;
  17. static final int TIME_DIALOG_ID = 0;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_basic_views);
  22. timePicker = (TimePicker) findViewById(R.id.timePicker);
  23. timePicker.setIs24HourView(true);
  24. showDialog(TIME_DIALOG_ID);
  25. }
  26. @Override
  27. protected Dialog onCreateDialog(int id)
  28. {
  29. switch(id){
  30. case TIME_DIALOG_ID:
  31. return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
  32. }
  33. return null;
  34. }
  35. private TimePickerDialog.OnTimeSetListener mTimeSetListener =
  36. new TimePickerDialog.OnTimeSetListener() {
  37. public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfHour)
  38. {
  39. hour = hourOfDay;
  40. minute = minuteOfHour;
  41. SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
  42. Date date = new Date(0,0,0, hour, minute);
  43. String strDate = timeFormat.format(date);
  44. Toast.makeText(getBaseContext(), "You have selected "+strDate,
  45. Toast.LENGTH_SHORT).show();
  46. }
  47. };
  48. }

运行效果

t1.png

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号