当前位置:网站首页>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;
}
}


边栏推荐
- 【日常积累 - 06】查看cuda和cudnn版本
- FileOutputStream(文件储存)与FileInputStream(文件读取)
- Sqlife (database)
- 贪心
- GestureOverlayView(手势识别2)
- [basic knowledge of deep learning - 41] quick start learning materials for deep learning
- HDU1171_ Big event in HDU [01 backpack]
- Arrayadapter (array adapter) and simpleadapter (simple adapter)
- [daily accumulation - 07] CUDA multi version switching
- Remember an unprepared internship interview
猜你喜欢

SQlife(数据库)

Combinatorics -- permutation and combination

GestureDetector(手势识别)

注入攻击

GestureOverlayView(手势识别2)

ToggleButton(按钮开关)
![[basic knowledge of deep learning - 45] distance calculation methods commonly used in machine learning](/img/6c/b0c2ea667ac361c13d38c8f5e6e5f1.png)
[basic knowledge of deep learning - 45] distance calculation methods commonly used in machine learning

Influxdb series (IV) TSM engine (storage principle)

Arrayadapter (array adapter) and simpleadapter (simple adapter)

JS 事件监听 鼠标 键盘 表单 页面 onclick onkeydown onChange
随机推荐
下放三星3J1传感器:代码暗示Pixel 7人脸识别安全性将大增
【深度学习基础知识 - 45】机器学习中常用的距离计算方法
【日常积累 - 06】查看cuda和cudnn版本
NAT 11.16
Gesturedetector (gesture recognition)
[basic knowledge of in-depth learning - 40] Why does CNN have more advantages than DNN in the field of images
三星将推多款RISC-V架构芯片,5G毫米波射频芯片会率先采用
Broadcastreceiver (broadcast)
Detailed explanation of the underlying data structure of redis
Systemservice (system service)
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte
Publish your own NPM component library
常见运算符9.21
Detailed interpretation of IEC104 protocol (I) protocol structure
It is said that Apple plans to buy some JDI factories with us $200million
mysql学习录(三)多表查询、子查询、分页查询、case语句、单行函数
[basic knowledge of deep learning - 43] concept of odds ratio
The valuation exceeds 15.6 billion yuan! Huaqin communication completed the round B financing of 1billion yuan! Qualcomm venture capital, Intel Capital led investment
pytorch tensor的基本函数
[basic knowledge of deep learning - 39] comparison of BN, LN and WN