zoukankan      html  css  js  c++  java
  • DatePicker输入日期控件的使用

       DataPicker(输入日期控件),DatePicker控件可用于输入日期,日期的范围是1900-1-1到2100-12-31.

    一、建立工程,如图

    二、activity_main.xml中代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        
        <DatePicker
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/datepicker"
            />
        <TimePicker 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/timepicker"
            />
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textview"
            android:textSize="18dp"
            
            />
        
    </LinearLayout>
    View Code

    三、MainActivity.java中代码

    package com.study.datetimepicker;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.DatePicker;
    import android.widget.DatePicker.OnDateChangedListener;
    import android.widget.TextView;
    import android.widget.TimePicker;
    import android.widget.Toast;
    import android.widget.TimePicker.OnTimeChangedListener;
    
    public class MainActivity extends Activity implements OnDateChangedListener,OnTimeChangedListener{
    
        private TextView textView;
        private DatePicker datePicker;
        private TimePicker timePicker;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            datePicker = (DatePicker)this.findViewById(R.id.datepicker);
            timePicker = (TimePicker)this.findViewById(R.id.timepicker);
            textView = (TextView)this.findViewById(R.id.textview);
            datePicker.init(2001, 1, 25, this);//初始化日期
            timePicker.setIs24HourView(true);//显示时间是否是按24小时制
            timePicker.setOnTimeChangedListener(this);//注册事件
            
        }
    
    
        @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;
        }
    
        //日期控件的触发
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
                    timePicker.getCurrentHour(), timePicker.getCurrentMinute());
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
            textView.setText(format.format(calendar.getTime()));
            
        }
    
        //时间空间的触发
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            Toast.makeText(MainActivity.this, "hourOfDay:" + hourOfDay + "  minute:" + minute, 1).show();
            
            
        }
    }
    View Code

    四、效果图

  • 相关阅读:
    读写分离之Atlas
    数组、集合和可变长参数
    史上最全的maven的pom.xml文件详解
    ArrayList集合与List与数组的区别
    ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
    Linux下Nagios的安装与配置
    Redis在windows下安装过程
    TortoiseGit-2.0.0.0-64bit问题
    丢用lamp手动安装apache php mysql
    手把手教你小程序对接微信支付
  • 原文地址:https://www.cnblogs.com/kingshow123/p/date_timepicker.html
Copyright © 2011-2022 走看看