简单的设置时间
本文主要涉及DatePickerDialog与TimePickerDialog的使用,来看一个小小程序吧!作为一个Android小小白,把两个类合在了一起使用
本来还在想难道两个类都要同时继承Activity?!然后想想想,最后试试试!!终于把书上讲得两个类拼在了一起使用,单独得来设置日期与时刻,
的确,很多东西要去尝试才能得出结果,哪怕是错误的,至少你知道了,目前你的办法是行不通的。多尝试!这里直接把文件名设置成了TimePickDialogExample
具体TimePickDialogExample.java代码如下:
1 package com.example.lenovo.A1103; 2 3 4 import android.app.TimePickerDialog; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.DatePicker; 10 import android.widget.TextView; 11 import android.widget.TimePicker; 12 13 import java.util.Calendar; 14 import java.util.Date; 15 import java.util.Locale; 16 17 public class TimePickerDialogExample extends AppCompatActivity { 18 private TextView showDate; 19 private Button setDate; 20 private TextView showTime; 21 private Button setTime; 22 private int year; 23 private int month; 24 private int day; 25 private int hours; 26 private int minus; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 33 showDate = (TextView) findViewById(R.id.showDate); 34 setDate = (Button) findViewById(R.id.setDate); 35 showTime = (TextView) findViewById(R.id.showTime); 36 setTime = (Button) findViewById(R.id.setTime); 37 38 Calendar myCalendar = Calendar.getInstance(Locale.CHINA); 39 Date myDate = new Date(); 40 myCalendar.setTime(myDate); 41 year = myCalendar.get(Calendar.YEAR); //获取Calendar对象中的年 42 month = myCalendar.get(Calendar.MONTH); 43 day = myCalendar.get(Calendar.DAY_OF_MONTH); 44 hours = myCalendar.get(Calendar.HOUR_OF_DAY); 45 minus = myCalendar.get(Calendar.MINUTE); 46 showDate.setText(year + "-" + (month + 1) + "-" + day); 47 showTime.setText(hours + ":" + minus); 48 49 setDate.setOnClickListener(new View.OnClickListener() { 50 @Override 51 public void onClick(View v) { 52 android.app.DatePickerDialog dpd = new android.app.DatePickerDialog(TimePickerDialogExample.this, 53 new android.app.DatePickerDialog.OnDateSetListener() { 54 @Override 55 public void onDateSet(DatePicker view, int myYear, int monthOfYear, int dayOfMonth) { 56 showDate.setText(myYear + "-" + (monthOfYear + 1) + "-" + dayOfMonth); 57 year = myYear; 58 month = monthOfYear; 59 day = dayOfMonth; 60 } 61 }, year, month, day); 62 //这是一整个构造函数,四个参数 63 dpd.show(); 64 } 65 }); 66 setTime.setOnClickListener(new View.OnClickListener() { 67 @Override 68 public void onClick(View v) { 69 TimePickerDialog tpd = new TimePickerDialog(TimePickerDialogExample.this, 70 new TimePickerDialog.OnTimeSetListener() { 71 @Override 72 public void onTimeSet(TimePicker view, int hourOfDay, int myMinute) { 73 showTime.setText(hourOfDay + ":" + myMinute); 74 } 75 }, hours, minus, false); 76 tpd.show();//显示TimePickerDialog组件 77 } 78 }); 79 80 } 81 82 }
layout/main_xml代码如下:
1 <LinearLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:orientation="vertical"> 6 <LinearLayout 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:orientation="horizontal"> 10 <TextView 11 android:id="@+id/showDate" 12 android:text="tv" 13 android:textSize="30sp" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:background="#ffff0101" /> 17 <TextView 18 android:id="@+id/showTime" 19 android:text="tv" 20 android:textSize="30sp" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:background="#fff010" 24 android:layout_marginLeft="30dp"/> 25 </LinearLayout> 26 <Button 27 android:layout_marginTop="30dp" 28 android:id="@+id/setDate" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="设置日期" /> 32 <Button 33 android:id="@+id/setTime" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:text="设置时间" 37 /> 38 39 </LinearLayout>
------------------------
没有谁的人生不是斩棘前行
----------------------------------------
JM