zoukankan      html  css  js  c++  java
  • DatePickerDialog和TimePickerDialog(基于对话框显示时间和日期)

    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;
    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();
    //显示日期对话框
    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;
    }
  • 相关阅读:
    SmartPlant Review 渲染模块低性能设置
    由浅入深:Python 中如何实现自动导入缺失的库?(转)
    itchat初步解读登录(转)
    转:【开源必备】常用git命令
    2.转发。基于itchat的微信消息同步机器人
    1、初学探讨PYTHON的itchat和wxpy两库
    学习git 新手。这个写的不错
    常见的内置错误及处理
    面试题记录1
    防抖
  • 原文地址:https://www.cnblogs.com/cct1314520/p/6426683.html
Copyright © 2011-2022 走看看