Android 开发 日期,时间拾取器

xml

添加了ScrollView滚动条,但是在滚动条内部必须要有LayOut布局工具,否则会闪退

时间和日期拾取器通过DatePicker和TimePicker控件实现

<?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:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date Time"
                />
        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                        <DatePicker
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/datePicker1"
                        >
                        </DatePicker>
                        <TimePicker
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/timePicker1"
                        >
                        </TimePicker>
                </LinearLayout>
        </ScrollView>


</LinearLayout>



MainActivity

package com.p3_3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import java.util.Calendar;

public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    Calendar calendar = Calendar.getInstance();//创建一个日历对象
    int year = calendar.get(Calendar.YEAR);//获取当前年份
    int month = calendar.get(Calendar.MONTH);//获取当前月份
    int day = calendar.get(Calendar.DAY_OF_MONTH);//获取当前日期
    int hour = calendar.get(Calendar.HOUR_OF_DAY);//获取当前小时数
    int minute = calendar.get(Calendar.MINUTE);//获取当前分钟数
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker1);
        TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker1);
        timePicker.setIs24HourView(true);



        //初始化日期拾取器,并在初始化时指定监听器
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
                MainActivity.this.year = year;
                MainActivity.this.month = month;
                MainActivity.this.day = day;
                show(year,month,day,hour,minute);
            }
        });
        //为日期拾取器设置监听器
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                MainActivity.this.hour = hour;
                MainActivity.this.minute = minute;
                show(year,month,day,hour,minute);
            }
        });

    }
    private void show(int year,int month,int day,int hour,int minute){
        String str = year + "年" + (month+1) + "月" + day + "日" + hour + ":" + minute;
        //因为month是0~11,所以需+1
        Toast.makeText(this,str,Toast.LENGTH_LONG);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值