A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的设置小结

  package com.haut.a07_timepicker;

  import android.app.Activity;

  import android.os.Bundle;

  import android.view.Menu;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  import android.widget.TimePicker;

  import android.widget.TimePicker.OnTimeChangedListener;

  import android.widget.Toast;

  public class MainActivity extends Activity {

  private TimePicker timePicker;

  private Button button;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  timePicker = (TimePicker) findViewById(R.id.timePickerId);

  button = (Button) findViewById(R.id.buttonId);

  // 为timePicker创建监听器

  TimePickerListener timeListener = new TimePickerListener();

  timePicker.setOnTimeChangedListener(timeListener);

  // 为button创建监听器

  ButtonListener buttonListener = new ButtonListener();

  button.setOnClickListener(buttonListener);

  // TimePicker默认显示当前时间,可以手动制定它的默认显示时间

  timePicker.setCurrentHour(12);

  timePicker.setCurrentMinute(0);

  // 设置显示格式为24小时制

  timePicker.setIs24HourView(true);

  }

  class TimePickerListener implements OnTimeChangedListener {

  public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

  // 使用Toast显示TimePicker的时间

  String time = hourOfDay + "点:" + minute + "分";

  Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();

  }

  }

  class ButtonListener implements OnClickListener {

  public void onClick(View v) {

  String time = timePicker.getCurrentHour() + "点:"

  + timePicker.getCurrentMinute() + "分";

  Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();

  }

  }

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.

  getMenuInflater().inflate(R.menu.activity_main, menu);

  return true;

  }

  }