时间日期控件
AnalogClock、DigitalClock 模拟时钟与数字时钟
时钟控件包括AnalogClock和DigitalClock,它们都负责显示时钟,所不同的是AnalogClock控件显示模拟时钟,且只显示时针和分针,而DigitalClock显示数字时钟,可精确到秒
以下模拟时钟的用法
目录结构
布局文件
<?xml version="1.0" encoding="utf-8"?>
<!-- AnalogClock与DigitalClock这两个时钟控件都不需要Java代码,
只要在layout的xml里插入以下代码即可自动显示时间-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 模拟时钟控件 -->
<AnalogClock android:id="@+id/analogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<!-- 数字时钟控件 -->
<DigitalClock android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<!-- AnalogClock与DigitalClock这两个时钟控件都不需要Java代码,
只要在layout的xml里插入以下代码即可自动显示时间-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 模拟时钟控件 -->
<AnalogClock android:id="@+id/analogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<!-- 数字时钟控件 -->
<DigitalClock android:id="@+id/digitalClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
运行结果
TimePicker与DatePicker 时间选择器与日期选择器
1 public class MainActivity extends Activity { 2 3 private int year; 4 private int month; 5 private int day; 6 private int hour; 7 private int minute; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 TimePicker timepicker=(TimePicker) findViewById(id.timePicker1); 14 DatePicker datepicker=(DatePicker) findViewById(id.datePicker1); 15 timepicker.setIs24HourView(true); 16 Calendar calendar=Calendar.getInstance();//获取当前时间的对象 17 year = calendar.get(Calendar.YEAR); 18 month = calendar.get(Calendar.MONTH); 19 day = calendar.get(Calendar.DAY_OF_MONTH); 20 hour = calendar.get(Calendar.HOUR_OF_DAY); 21 minute = calendar.get(Calendar.MINUTE); 22 //为日期初始化 并设置改变监听器 23 datepicker.init(year, month, day, new OnDateChangedListener() { 24 25 @Override//日期改变监听 26 public void onDateChanged(DatePicker view, int year, int monthOfYear, 27 int dayOfMonth) { 28 MainActivity.this.year=year;//得到改变后的值 29 MainActivity.this.month=monthOfYear; 30 MainActivity.this.day=dayOfMonth; 31 show(year,monthOfYear,dayOfMonth,hour,minute); 32 } 33 }); 34 //为时间设置监听器 35 timepicker.setOnTimeChangedListener(new OnTimeChangedListener() { 36 37 @Override 38 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 39 MainActivity.this.hour=hourOfDay; 40 MainActivity.this.minute=minute; 41 show(year,month,day,hourOfDay,minute); 42 } 43 }); 44 } 45 //定义方法显示 46 private void show(int year,int month,int day,int hour,int minute){ 47 String str=year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute; 48 Toast.makeText(this, str,0).show(); 49 } 50 }
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/timePicker1" android:layout_marginTop="20dp" /> </RelativeLayout>
效果如图:
TimePickerDialog与DatePickerDialog 时间选择对话框与日期选择对话框
1 public class MainActivity2 extends Activity { 2 3 private int year; 4 private int month; 5 private int day; 6 private int hour; 7 private int minute; 8 private TextView tv; 9 private TextView tv2; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main2); 14 tv = (TextView) findViewById(id.textView1); 15 tv2 = (TextView) findViewById(id.textView2); 16 } 17 public void button_onClick(View view){//按钮单击 18 Calendar c=Calendar.getInstance(); 19 hour=c.get(Calendar.HOUR_OF_DAY); 20 minute=c.get(Calendar.MINUTE); 21 //时间对话框 22 TimePickerDialog dialog=new TimePickerDialog(this, new OnTimeSetListener() { 23 24 @Override 25 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 26 tv.setText(hourOfDay +":" +minute); 27 } 28 }, hour, minute,true); 29 dialog.show(); 30 } 31 32 public void button_onClick2(View view){//按钮单击 33 Calendar c=Calendar.getInstance(); 34 year=c.get(Calendar.YEAR); 35 month=c.get(Calendar.MONTH); 36 day=c.get(Calendar.DAY_OF_MONTH); 37 DatePickerDialog ap=new DatePickerDialog(this,new OnDateSetListener() { 38 //日期对话框 39 @Override 40 public void onDateSet(DatePicker view, int year, int monthOfYear, 41 int dayOfMonth) { 42 tv2.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"); 43 } 44 }, year, month, day); 45 ap.show(); 46 } 47 }
布局只需要按钮显示文本
效果如图: