android时钟显示

在android应用中时常用的时间的显示,下面是一个简单的时钟显示设计。

页面显示:

android时钟显示_第1张图片android时钟显示_第2张图片android时钟显示_第3张图片

布局设计:

main.xml:



	
	

	

	

代码显示:

main.java:

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
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 Main extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	private Button button1, button2;
	private int hourOfDay, minute;
	private int year, monthOfYear, dayOfMonth;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.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);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			//
			TimePickerDialog timePickerDialog = new TimePickerDialog(Main.this,
					new MyTimePickerDialog(), hourOfDay, minute, true);
			timePickerDialog.show();// 显示对话框
			break;
		case R.id.button2:
			DatePickerDialog datePickerDialog = new DatePickerDialog(Main.this,
					new MyDatePickerDialog(), year, monthOfYear, dayOfMonth);
			datePickerDialog.show();// 显示对话框
			break;
		}
	}

	public class MyDatePickerDialog implements
			DatePickerDialog.OnDateSetListener {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			Toast.makeText(
					Main.this,
					"year:" + year + "monthOfYear:" + monthOfYear
							+ "dayOfMonth:" + dayOfMonth, 1).show();
		}

	}

	public class MyTimePickerDialog implements
			TimePickerDialog.OnTimeSetListener {

		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			Toast.makeText(Main.this,
					"hourOfDay:" + hourOfDay + "minute:" + minute, 1).show();
		}

	}
}

你可能感兴趣的:(android时钟显示)