AnalogClock和DigitalClock时间和日期控件
2014-03-30 16:16 kingshow 阅读(371) 评论(0) 编辑 收藏 举报
AnalogClock和DigitalClock(显示时钟的控件).
一、建立工程,如图
二、activity_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="fill_parent" android:layout_height="fill_parent"> <AnalogClock android:layout_width="fill_parent" android:layout_height="wrap_content" /> <DigitalClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button1" android:text="显示TimePickerDialog" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button2" android:text="显示DatePickerDialog" /> </LinearLayout>
三、MainActivity.java中代码
package com.study.analogcolck; import java.util.Calendar; import android.os.Bundle; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button button1,button2; private int hourOfDay,minute; private int year,monthOfYear,dayOfMonth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)this.findViewById(R.id.button1); button2 = (Button)this.findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); //获得当前的时间,获得小时和分钟 Calendar calendar = Calendar.getInstance(); hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(calendar.DAY_OF_MONTH); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new MyTimePickerDialog(), hourOfDay, minute, true); timePickerDialog.show(); break; case R.id.button2: DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new MyDatePickerDialog(), year, monthOfYear, dayOfMonth); datePickerDialog.show(); break; default: break; } } public class MyTimePickerDialog implements OnTimeSetListener{ @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, "H:" + hourOfDay +" minute:" + minute, 1).show(); } } public class MyDatePickerDialog implements OnDateSetListener{ @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, year+"年" + monthOfYear+" 月" +dayOfMonth+ " 日", 1).show(); } } }
四、效果图