当前位置:操作系统 > 安卓/Android >>

A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的设置

目标:学习时间日期和时钟的设置

picker的计算机专业解释是“选择器”。

简单翻译一下:


TimePicker 时间选择器

DatePicker 日期选择器

AnalogClock 模拟时钟

DigitalClock 数字时钟

 

一、TimePicker

1.TimePicker使用的易做图接口是OnTimeChangedListener

2.TimePicker默认显示系统当前时间,可以使用setCurrentHour和setCurrentMinute两个方法设置默认显示时间

3.可使用setIs24HourView方法设置TimePicker以24小时制显示

4.获取TimePicker的当前时间,使用getCurrentHour和getCurrentMinute两个方法

模拟器android4.2显示效果(非24小时制):

 \
 


真机android2.3.7显示效果(非24小时制):

 \
 


真机android2.3.7显示效果(24小时制):

 \
 

 

Java代码:


[java]
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; 
    } 
 

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.
  getMenuInfl

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,