zoukankan      html  css  js  c++  java
  • Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <EditText
            android:id="@+id/input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="datetime" />
    
        <LinearLayout
            android:id="@+id/LinearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
            <DatePicker
                android:id="@+id/mydp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TimePicker
                android:id="@+id/mytp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
    </LinearLayout>

    MainActivity.java

    public class MainActivity extends Activity {
        private EditText myInput=null;
        private TimePicker mytp=null;
        private DatePicker mydp=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.myInput=(EditText)super.findViewById(R.id.input);
            this.mytp=(TimePicker)super.findViewById(R.id.mytp);
            this.mydp=(DatePicker)super.findViewById(R.id.mydp);
            this.mytp.setIs24HourView(true);//设置为24小时制
            this.mytp.setOnTimeChangedListener(new OnTimeChangedListenerImpl());//添加时间变更响应事件
            this.mydp.init(this.mydp.getYear(), 
                    this.mydp.getMonth(), 
                    this.mydp.getDayOfMonth(), 
                    new OnDateChangeLinstenerImpl());//添加日期变更响应事件
            this.setDateTime(); //设置初始的日期事件
        }
        private class OnTimeChangedListenerImpl implements OnTimeChangedListener{
    
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                // 当时间改变时触发的事件
                setDateTime();
            }
            
        }
        
        private class OnDateChangeLinstenerImpl implements OnDateChangedListener{
    
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // 当日期改变时触发的事件
                setDateTime();
            }
            
        }
        
        public void setDateTime(){
            this.myInput.setText(this.mydp.getYear()+"-"+(this.mydp.getMonth()+1)+"-"+this.mydp.getDayOfMonth()+" "+this.mytp.getCurrentHour()+":"+this.mytp.getCurrentMinute());
        }
  • 相关阅读:
    Python分析网页中的<a>标签
    Python3.X爬虫
    如何使你的窗体样式固定,不因系统设定而变化
    C# 窗体内有子控件时鼠标检测
    VS新建项目工具箱图标丢失问题
    在c#中使用bitblt显示图片
    DotNetBar 中Ribbon汉化
    汉化DotNetBar中控件的系统文本
    castle动态代理的使用
    FastReport 套打全攻略
  • 原文地址:https://www.cnblogs.com/taobox/p/3342746.html
Copyright © 2011-2022 走看看