当前位置:网站首页>DatePicker(日期选择器)与TimePicker(时间选择器)
DatePicker(日期选择器)与TimePicker(时间选择器)
2022-07-27 17:02:00 【Ashurol】
public class MainActivity extends Activity {
private DatePicker date_picker;
private TimePicker time_picker;
private TextView tv_date;
private TextView tv_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化
date_picker=(DatePicker) findViewById(R.id.date);
time_picker=(TimePicker) findViewById(R.id.time);
tv_date=(TextView) findViewById(R.id.tv_date);
tv_time=(TextView) findViewById(R.id.tv_time);
//获取当前系统的年月日时分信息
Calendar c=Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
tv_date.setText("现在日期:"+year+"-"+(month+1)+"-"+day);
tv_time.setText("现在时间:"+hour+":"+minute);
//初始化日期
date_picker.init(year, month, day, new OnDateChangedListener() {
//监听日期的改变
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this, "现在时间:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, 0).show();
tv_date.setText("现在日期:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
});
//设置时间为24小时制
time_picker.setIs24HourView(true);
time_picker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
tv_time.setText("现在时间:"+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;
}
}
对话框形式的时间选择器和日期选择器
public class MainActivity extends Activity implements android.view.View.OnClickListener{
private Button date_button;
private Button time_button;
private Calendar calendar;
private TextView tv_date;
private TextView tv_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
date_button=(Button) findViewById(R.id.date_button);
time_button=(Button) findViewById(R.id.time_button);
tv_date=(TextView) findViewById(R.id.tv_date);
tv_time=(TextView) findViewById(R.id.tv_time);
date_button.setOnClickListener(this);
time_button.setOnClickListener(this);
//获取当前的年月日时分信息
calendar=Calendar.getInstance();
}
@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;
}
private void showDateDialog()
{
DatePickerDialog date_dialog=new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
tv_date.setText("现在日期:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
date_dialog.show();
}
private void showTimeDialog()
{
TimePickerDialog time_dialog=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
tv_time.setText("现在时间:"+hourOfDay+":"+minute);
}
}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
time_dialog.show();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.date_button:
showDateDialog();
break;
case R.id.time_button:
showTimeDialog();
break;
}
}


边栏推荐
- 4 轮拿下字节 Offer,面试题复盘
- I want to consult. Our maxcompute spark program needs to access redis, development environment and production environment redis
- TSMC 5nm is about to mass produce: Apple A14 monopolizes 70% of the production capacity, and Huawei Kirin 1020 takes 30%
- Under the heat wave of Web3.0, the ecological shock of Mensa struck
- 请问创建MySQL数据源资源组必须要选择新建独享数据集成资源组才可用?还是使用公共资源组就可以?谢谢
- 27. Basics of golang - mutex lock, read / write lock
- OPPO发布首款AR眼镜,宣布未来3年投入500亿进行研发
- Anaconda下安装Talib库
- c语言:14、预处理
- Intel releases horse ridge chip: 22nm process, which can control multiple qubits
猜你喜欢

IIS 发生未知FastCGI错误:0x80070005

A low code development platform that brings high-value user experience

c语言:12、gdb工具调试c程序

c语言:9、main函数中的return

c语言:15、结构体

下放三星3J1传感器:代码暗示Pixel 7人脸识别安全性将大增

IEC104 规约详细解读(一) 协议结构

C language: 9. Return in main function

A lock faster than read-write lock. Don't get to know it quickly

Embedded C language structure
随机推荐
5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中
Surpass Huawei? Ericsson has won more than 75 5g commercial contracts
The first Xiaolong 765G! Redmi K30 5g release: support 5g dual-mode 120Hz screen, priced from 1999 yuan
Hardware acceleration of zero knowledge proof
大佬们,ORACLE CDC,本地运行,老是遇到这个An exception occurred in
C language: C language code style
S32K系列芯片--简介
c语言:5、多维数组
c语言:9、main函数中的return
[basic knowledge of deep learning - 49] kmeans
Debian recaptured the "debian.community" domain name, but it's still not good to stop and rest
Complex number proof of solvability of regular 17 sided ruler and gauge drawing
电商商城小程序项目完整源码(微信小程序)
传苹果计划以2亿美元购买JDI部分工厂
Hdu1573 x problem [univariate linear congruence equations]
【深度学习基础知识 - 48】贝叶斯网络的特点
开启和禁用hyper-v
Take byte offer in four rounds and answer the interview questions
Fzu1669 right angled triangle
爱立信承认在中国等五国行贿,向美支付10.6亿美元罚款