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了。
在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了。
相关文章推荐
- Android获取DatePicker中的时间,并转换成毫秒数
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- Android的日期DatePickerDialog和时间TimePickerDialog简单使用
- Datepicker和Timepicker简单使用与样式改变,以及系统时间获取
- Android时间日期拾取器学习使用(DatePicker、TimePicker)
- DatePicker获取时间时,如何转换时区的问题
- Android 日期控件(DatePickerDialog)与时间控件(TimePickerDialog)的使用
- Android月历控件(DatePicker)和时间控件(TimePicker)的使用
- Android 使用DatePicker以及TimePicker显示当前日期和时间
- android如何如何获取客户是否使用过软件或是获取客户上一次使用的时间
- android时间控件DatePicker使用实例
- android开发中DatePicker控件的使用-------->时间的设置
- android 时间日期控件的使用(DatePicker、TimePicker、DatePickerDialog、TimePickerDialog、AnalogClock、DigitalClock)
- Android --- 获取12/24制系统时间(使用SimpleDateFormat和Calendar)
- Android DatePicker控件获取时间月份少1
- Android DatePicker日期选择器、TimePicker时间选择器的使用
- Android——使用DatePicker和TimePicker显示当前日期和时间
- 时间对话框 android DatePicker的使用
- android------(设置时间)DatePicker对象的使用
- Android开发中DatePicker日期与时间控件实例代码