时间与日期--MarsChen Android 开发教程学习笔记

这节课是讲如何在设备中添加时间和日期的设定组块,让用户可以在开发者的应用中设定时间和日期。要点分别为:TimePicker(时间设定器) 、OnTimeChangeListener(时间改变监听器) 、DatePicker 日期设定器)和
AnalogClock(模拟时钟显示组件)。
T imePicker
在XML文件中,先声明<TimePicker/>必须在模拟器中运行,才能看清楚效果。
在JAVA代码中,先声明timepicker对象,通过绑定OnTimeChangedListener 监听器得知用户改变情况。在onTimeChange方法中,view 对象代表出发listener 的timepicker对象,houerOfDay 代表用户选择的小时,minute 代表用户选择的分钟。
若是将时间设定为24小时制,在JAVA代码中的onCreater 中调用setIs24HourView(boolean) 方法即可。
以上的缺点在于,每次用户改变时间都会予以显示,但是用户滑动时间设置组件的时候是有过程的,如果不断打印结果会显得非常乱,此时,添加一个按钮组件,当用户设定完时间,点击该按钮,再打印时间信息。做法是:在XML文件中添加按钮组件,在XML文件中设置button 对象,并设置继承自OnClickListener 的ButtonListener 监听器,其中hour 和minute 可分别用getCurrentHour() 和getCurrentMinute() 方法获得用户设定完成后的时间数值。
在界面中,时钟默认显示的时间是和模拟器或现实设备的系统时间相同,若要改变时间,可以通过在JAVA代码中调用setCurrentHour() 和setCurrentMinute() 方法实现。
OnTimeChangedListener
与TimePicker一起使用,主要监测用户对时间和日期的改变。
DatePicker
是一个用于用户设定日期的模块。
在布局文件XML中加入<DatePicker /> ,JAVA 代码中加入datepicker 对象。另外,加入按钮组件,也在JAVA 代码中加入button 对象,组成一个获得用户设定日期值的模式。但是不能用对DatePicker 的监听器,只能添加按钮的监听器ButtonListener (继承自OnClickListener ),在此监听器的onClick 方法中,分别通过getYear() 、getMounth() 和getDayOfMuouth() 方法获取用户设定完毕后的年、月、日的值。但是注意,以上所用的方法的月份是从0 开始算起的,所以打印行代码中值还是要加1 。
若要使DatePicker 在用户未设置之前显示的是开发者设定的时间,可在onCreate 方法中对datpicker 对象调用updateDate(int year, int mouth, int day) 方法。
AnalogClock(模拟时钟)
在XML中添加就可以啦。
在XML 文件中的代码:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.shumin.lbs04.MainActivity" >

    <TimePicker 
        android:id="@+id/timepicker"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />

    <DatePicker
        android:id="@+id/datepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/timepicker"
        android:layout_alignParentBottom="true" />
    
    <Button 
        android:id="@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/timepicker"
        android:layout_above="@id/datepicker"
        android:text="B"/>
    
    
</RelativeLayout>


在JAVA 文件中的代码:

package com.shumin.lbs04;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;


public class MainActivity extends ActionBarActivity {

private TimePicker timepicker;
private DatePicker datepicker;
private Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timepicker=(TimePicker)findViewById(R.id.timepicker);
timePickerListener timepickerlistener =new timePickerListener();
datepicker=(DatePicker)findViewById(R.id.datepicker);
button=(Button)findViewById(R.id.button);
timepicker.setOnTimeChangedListener(timepickerlistener);
buttonListener buttonlistener=new buttonListener();
button.setOnClickListener(buttonlistener);

}
class buttonListener implements OnClickListener{

@Override
public void onClick(View v) {
int year=datepicker.getYear();
int month=datepicker.getMonth();
int day=datepicker.getDayOfMonth();
System.out.println(year+"年"+month+"月"+day+"日。");
}

}
class timePickerListener implements OnTimeChangedListener{

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
System.out.println(hourOfDay+"时"+minute+"分。");
}

}

@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值