• DatePicker和TimePicker的用法


    xml布局:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3   android:layout_width="fill_parent"
     4   android:layout_height="fill_parent"
     5   android:orientation="vertical" >
     6  
     7   <TextView
     8     android:id="@+id/textView1"
     9     android:layout_width="wrap_content"
    10     android:layout_height="wrap_content"
    11     android:text="请选择购买本书的具体时间" />
    12  
    13   <DatePicker
    14     android:id="@+id/datePicker"
    15     android:layout_width="wrap_content"
    16     android:layout_height="wrap_content"
    17     android:layout_gravity="center_horizontal" />
    18  
    19   <TimePicker
    20     android:id="@+id/timePicker"
    21     android:layout_width="wrap_content"
    22     android:layout_height="wrap_content"
    23     android:layout_gravity="center_horizontal" />
    24  
    25   <EditText
    26     android:id="@+id/show"
    27     android:layout_width="wrap_content"
    28     android:layout_height="wrap_content"
    29     android:cursorVisible="false"
    30     android:editable="false" />
    31  
    32 </LinearLayout>

    Java代码:

     1 public class AndroidDemo5Activity extends Activity {
     2  
     3  // 记录当前的时间
     4  private int year;
     5  private int month;
     6  private int day;
     7  private int hour;
     8  private int minute;
     9  
    10  @Override
    11  protected void onCreate(Bundle savedInstanceState) {
    12  // TODO Auto-generated method stub
    13  super.onCreate(savedInstanceState);
    14  setContentView(R.layout.main);
    15  DatePicker date = (DatePicker) findViewById(R.id.datePicker);
    16  TimePicker time = (TimePicker) findViewById(R.id.timePicker);
    17  // 或许当前的年月日,小时,分钟
    18  Calendar ca = Calendar.getInstance();
    19  year = ca.get(Calendar.YEAR);
    20  month = ca.get(Calendar.MONTH);
    21  day = ca.get(Calendar.DAY_OF_MONTH);
    22  hour = ca.get(Calendar.HOUR);
    23  minute = ca.get(Calendar.MINUTE);
    24  
    25  // 初始化DatePicker
    26  date.init(year, month, day, new OnDateChangedListener() {
    27  
    28   @Override
    29   public void onDateChanged(DatePicker arg0, int year, int month,
    30    int day) {
    31   AndroidDemo5Activity.this.year = year;
    32   AndroidDemo5Activity.this.month = month;
    33   AndroidDemo5Activity.this.day = day;
    34   // 显示当前时间和日期
    35   showDate(year, month, day, hour, minute);
    36   }
    37  
    38  });
    39  
    40  // 为TimerPicker指定事件监听器
    41  time.setOnTimeChangedListener(new OnTimeChangedListener() {
    42  
    43   @Override
    44   public void onTimeChanged(TimePicker arg0, int hour, int minute) {
    45   AndroidDemo5Activity.this.hour = hour;
    46   AndroidDemo5Activity.this.minute = minute;
    47   }
    48  });
    49  }
    50  
    51  protected void showDate(int year2, int month2, int day2, int hour2,
    52   int minute2) {
    53  EditText text = (EditText) findViewById(R.id.show);
    54  text.setText("您的购买时间为:" + year2 + "年" + month2 + "月" + day2 + "日"
    55   + hour2 + "时" + minute2 + "分");
    56  
    57  }
    58 }

    效果:

  • 相关阅读:
    性能测试必备知识(10)- Linux 是怎么管理内存的?
    stat 命令家族(4)- 详解 iostat
    stat 命令家族(3)- 详解 mpstat
    stat 命令家族(2)- 详解 pidstat
    性能分析(6)- 如何迅速分析出系统 CPU 的瓶颈在哪里
    性能分析(5)- 软中断导致 CPU 使用率过高的案例
    实体类转xml
    运气一直好,就不只是运气了——记中学七年
    (数据科学学习手札93)利用geopandas与PostGIS进行交互
    JVM系列之一:内存区域和内存溢出
  • 原文地址:https://www.cnblogs.com/solar-sys/p/5990862.html
Copyright © 2020-2023  润新知