当前位置:网站首页>Datepicker and TimePicker
Datepicker and TimePicker
2022-07-27 19:53: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);
// initialization
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);
// Obtain the time information of the current system
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(" Current date :"+year+"-"+(month+1)+"-"+day);
tv_time.setText(" present time :"+hour+":"+minute);
// Initialization date
date_picker.init(year, month, day, new OnDateChangedListener() {
// Change of monitoring date
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this, " present time :"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, 0).show();
tv_date.setText(" Current date :"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
});
// Set the time to 24 hourly
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(" present time :"+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;
}
}
Time selector and date selector in the form of dialog box
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);
// Get the current information of month, day, and time
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(" Current date :"+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(" present time :"+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;
}
}


边栏推荐
- 5W bonus pool / for colleges and universities, 2022 legal science and technology innovation competition is in progress
- MarqueeTextview(跑马灯)
- 被动收入:回归原始且安全的两种赚取方法
- 【深度学习基础知识 - 42】逻辑回归详解
- 【深度学习基础知识 - 47】贝叶斯网络与朴素贝叶斯
- Togglebutton (button switch)
- [daily accumulation - 07] CUDA multi version switching
- Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased
- SQlife(数据库)
- 文件操作防护
猜你喜欢

Sqlife (database)

VS2017#include 'xxx.h'
![[basic knowledge of deep learning - 37] solve the imbalance between positive and negative samples](/img/71/4052607951eb52862a6fd36366f216.png)
[basic knowledge of deep learning - 37] solve the imbalance between positive and negative samples

DatePicker(日期选择器)与TimePicker(时间选择器)

GestureOverlayView(手势识别2)

SharePreference(存储)

Map and set

RadioGroup(单选框)

Complex number proof of solvability of regular 17 sided ruler and gauge drawing

rxbinding
随机推荐
[basic knowledge of deep learning - 45] distance calculation methods commonly used in machine learning
What's new in helix QAC 2022.2, the ace code static testing tool (1)
【深度学习基础知识 - 49】Kmeans
内置模块10.18
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte
Map and set
Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased
容器总结
focal loss
NAT 11.16
[daily accumulation - 06] view CUDA and cudnn versions
下放三星3J1传感器:代码暗示Pixel 7人脸识别安全性将大增
JS find all nodes sibling childNodes children
【深度学习基础知识 - 44】逻辑回归实现多分类的方法
【深度学习目标检测系列 - 01】目标检测是什么
Transaction log full problem handling in sqlserver 2008
[basic knowledge of in-depth learning - 40] Why does CNN have more advantages than DNN in the field of images
Complex number proof of solvability of regular 17 sided ruler and gauge drawing
Oppo released the first AR glasses and announced that it would invest 50billion in research and development in the next three years
Fabric上搭建Hyperledger caliper进行性能测试