您的位置:首页 > 移动开发 > Android开发

android中如何使用DatePicker获取时间

2014-01-23 11:30 639 查看
话不多说,直接看代码:

在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" >

       <TextView  

              android:id="@+id/date"

              android:textSize="16sp"

              android:layout_width="fill_parent" 

              android:layout_height="wrap_content" />

       <Button

              android:id="@+id/datepicker"

              android:text="选择日期"

              android:layout_width="fill_parent " 

              android:layout_height="wrap_content" />

</LinearLayout>

1、TextView中显示选择的时间  2、Button中添加监听器,用于显示DatePicker,和获取选择的时间;

在DateActivity.java中:

import java.util.Calendar;

import android.os.Bundle;

import android.app.Activity;

import android.app.DatePickerDialog;

import android.app.DatePickerDialog.OnDateSetListener;

import android.app.Dialog;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.TextView;

public class DateActivity extends Activity {

private TextView date;
private Button datePicker;

// 用来保存年月日:
private int mYear;
private int mMonth;
private int mDay;
// 声明一个独一无二的标识,来作为要显示DatePicker的Dialog的ID:
static final int DATE_DIALOG_ID = 0;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

date = (TextView) findViewById(R.id.date);

datePicker = (Button) findViewById(R.id.datepicker);

datePicker.setOnClickListener(new btnDow_OnClickListener());

// 获得当前的日期:
final Calendar currentDate = Calendar.getInstance();

mYear = currentDate.get(Calendar.YEAR);
mMonth = currentDate.get(Calendar.MONTH);
mDay = currentDate.get(Calendar.DAY_OF_MONTH);
// 设置文本的内容:
date.setText(new StringBuilder().append(mYear).append("年")
.append(mMonth + 1).append("月")// 得到的月份+1,因为从0开始
.append(mDay).append("日"));

}

public class btnDow_OnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// 调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,
// 并回调用 onCreateDialog(int)回调函数来请求一个Dialog
showDialog(DATE_DIALOG_ID);

}
}

// 需要定义弹出的DatePicker对话框的事件监听器:
private DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;

String str1 = new String(new StringBuilder().append(mYear)
.append("年").append(mMonth + 1).append("月")// 得到的月份+1,因为从0开始
.append(mDay).append("日"));

// 设置文本的内容:
date.setText(str1);
}
};

/**
* 当Activity调用showDialog函数时会触发该函数的调用:

*/
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}


这样就可以使用DatePicker了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息