如何在adapter里调用显示日期控件
展开全部
参考代码:
/**
* 弹出日期对话框
* @param textView
* @param date 默认日期
*/
public void showDateDialog(TextView textView, String date) {
txtDatePicker = textView;
calendar = Calendar.getInstance();
if (!toolUtil.isBlank(date)) {
try {calendar.setTime(dateUtil.formatDate().parse(date));} catch (Exception e) {}
}
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
txtDatePicker.setText(dateUtil.formatDate().format(calendar.getTime()));
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
/**
* 弹出时间对话框
* @param textView
*/
public void showTimeDialog(TextView textView) {
txtDatePicker = textView;
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
txtDatePicker.setText((hourOfDay < 10 ? "0" + hourOfDay : hourOfDay) + ":" + (minute < 10 ? "0" + minute : minute) + ":00");
}
}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();
}
2017-06-02
展开全部
package com.example.timer;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
public class MainActivity extends Activity {
int year = 0;
int monthOfYear = 0;
int dayOfMonth = 0;
int minute = 0;
int houre = 0;
TextView showDate = null;
TextView showtime = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
showDate(year, monthOfYear + 1, dayOfMonth);
showTime(houre, minute);
}
private void initView() {
// 日期控件对象
DatePicker date = (DatePicker) findViewById(R.id.main_dp);
// 获得日历对象
Calendar c = Calendar.getInstance();
// 获取当前年份
year = c.get(Calendar.YEAR);
// 获取当前月份
monthOfYear = c.get(Calendar.MONTH);
// 获取当前月份的天数
dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
// 获取当前的小时数
houre = c.get(Calendar.HOUR_OF_DAY);
// 获取当前的分钟数
minute = c.get(Calendar.MINUTE);
// 时间显示的文本对象
showDate = (TextView) findViewById(R.id.main_tv_showdate);
// 为日期设置监听事件
date.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
MainActivity.this.year = year;
MainActivity.this.monthOfYear = monthOfYear;
MainActivity.this.dayOfMonth = dayOfMonth;
showDate(year, monthOfYear + 1, dayOfMonth);
}
});
// 显示时间的文本控件
showtime = (TextView) findViewById(R.id.main_tv_showtime);
// 时间显示的控件
TimePicker time = (TimePicker) findViewById(R.id.main_tp_showTime);
// 为时间控件设置监听事件
time.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
showTime(hourOfDay, minute);
}
});
}
//显示日期的方法
private void showDate(int year, int monthOfYear, int dayOfMonth) {
showDate.setText("日期是:" + year + "年" + monthOfYear + "月" + dayOfMonth
+ "日");
}
//显示时间的方法
private void showTime(int houre2, int minute2) {
showtime.setText("时间是:" + houre2 + "时" + minute2 + "分");
}
}
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
public class MainActivity extends Activity {
int year = 0;
int monthOfYear = 0;
int dayOfMonth = 0;
int minute = 0;
int houre = 0;
TextView showDate = null;
TextView showtime = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
showDate(year, monthOfYear + 1, dayOfMonth);
showTime(houre, minute);
}
private void initView() {
// 日期控件对象
DatePicker date = (DatePicker) findViewById(R.id.main_dp);
// 获得日历对象
Calendar c = Calendar.getInstance();
// 获取当前年份
year = c.get(Calendar.YEAR);
// 获取当前月份
monthOfYear = c.get(Calendar.MONTH);
// 获取当前月份的天数
dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
// 获取当前的小时数
houre = c.get(Calendar.HOUR_OF_DAY);
// 获取当前的分钟数
minute = c.get(Calendar.MINUTE);
// 时间显示的文本对象
showDate = (TextView) findViewById(R.id.main_tv_showdate);
// 为日期设置监听事件
date.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
MainActivity.this.year = year;
MainActivity.this.monthOfYear = monthOfYear;
MainActivity.this.dayOfMonth = dayOfMonth;
showDate(year, monthOfYear + 1, dayOfMonth);
}
});
// 显示时间的文本控件
showtime = (TextView) findViewById(R.id.main_tv_showtime);
// 时间显示的控件
TimePicker time = (TimePicker) findViewById(R.id.main_tp_showTime);
// 为时间控件设置监听事件
time.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
showTime(hourOfDay, minute);
}
});
}
//显示日期的方法
private void showDate(int year, int monthOfYear, int dayOfMonth) {
showDate.setText("日期是:" + year + "年" + monthOfYear + "月" + dayOfMonth
+ "日");
}
//显示时间的方法
private void showTime(int houre2, int minute2) {
showtime.setText("时间是:" + houre2 + "时" + minute2 + "分");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询