• Android-TimePicker/DatePicker时间选择器Demo


    代码

    package com.lxt008;
    
    import java.util.Calendar;
    
    import com.lxt008.R;
    
    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.TimePickerDialog;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.TextView;
    import android.widget.TimePicker;
    
    public class Activity01 extends Activity
    {
        TextView    m_TextView;
        //声明DatePicker对象
        DatePicker    m_DatePicker;
        //声明TimePicker对象
        TimePicker    m_TimePicker;
        Button         m_dpButton;
        Button         m_tpButton;
        //java中的日历类
        Calendar     c;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            c=Calendar.getInstance();
            
            m_TextView= (TextView) findViewById(R.id.TextView01);
            m_dpButton = (Button)findViewById(R.id.button1);
            m_tpButton = (Button)findViewById(R.id.button2);
            
            //获取DatePicker对象
            m_DatePicker = (DatePicker) findViewById(R.id.DatePicker01);
            //将日历初始化为当前系统时间,并设置其事件监听
            m_DatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    //当日期更改时,在这里处理
                    //c.set(year, monthOfYear, dayOfMonth);
                }
            });
    
            //获取TimePicker对象
            m_TimePicker = (TimePicker) findViewById(R.id.TimePicker01);
            //设置为24小时制显示
            m_TimePicker.setIs24HourView(true);
    
            //监听时间改变
            m_TimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                @Override
                public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
                {
                    //时间改变时处理
                    //c.set(year, month, day, hourOfDay, minute, second);
                }
            });
            
            
            m_dpButton.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v)
                {
                    new DatePickerDialog(Activity01.this,
                        new DatePickerDialog.OnDateSetListener()
                        {
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                            {
                                //设置日历
                            }
                        },c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
            
            m_tpButton.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v)
                {
                    new TimePickerDialog(Activity01.this,
                        new TimePickerDialog.OnTimeSetListener()
                        {
                            public void onTimeSet(TimePicker view, int hourOfDay,int minute)
                            {
                                //设置时间
                            }
                        },c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
                }
            });
        }
    }

    布局文件

    <?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"
        >
    <TextView  
          android:id="@+id/TextView01"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
        <DatePicker
          android:id="@+id/DatePicker01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
        >
        </DatePicker>
        <TimePicker
          android:id="@+id/TimePicker01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
        >
        </TimePicker>
        <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="设置日期"
        >
        </Button>
        <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="设置时间"
        >
        </Button>
    </LinearLayout>
  • 相关阅读:
    前端事件系统(四)
    前端事件系统(三)
    前端事件系统(二)
    前端事件系统(一)
    前端图片选择问题
    浅谈前端移动端页面开发(布局篇)
    诙谐论设计模式(一)——创建型设计模式
    总结三年未满的工作经历,写下自己对技术的坚持
    java内存优化牛刀小试
    将上下文融入知识图谱以进行常识推理
  • 原文地址:https://www.cnblogs.com/spadd/p/4189902.html
Copyright © 2020-2023  润新知