码迷,mamicode.com
首页 > 其他好文 > 详细

日历 日期时间选择器

时间:2016-05-10 20:38:13      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:

1.日历

技术分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<CalendarView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:firstDayOfWeek="2"
        android:maxDate="12/31/2016"
        android:minDate="01/01/2016"
        android:id="@+id/cv_1"
        android:background="#999"
        android:visibility="gone">
    </CalendarView>

</LinearLayout>
View Code

2.日期选择器

技术分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<DatePicker
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:maxDate="12/31/2023"
        android:minDate="01/01/2000"
        android:calendarViewShown="false"
        android:id="@+id/dp_1"
        android:visibility="gone"
        >
    </DatePicker>

</LinearLayout>
View Code

3.时间选择器

技术分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tp_1"
        >
    </TimePicker>
</LinearLayout>
View Code

4.代码试行命令

技术分享
package com.example.administrator.testapp2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/05/07.
 */
public class TestActivity2 extends AppCompatActivity{

    CalendarView cv_1;
    DatePicker dp_1;
    TimePicker tp_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);

        cv_1 = (CalendarView)findViewById(R.id.cv_1);

        cv_1.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

                Toast.makeText(TestActivity2.this, "选中的日期是:"+year+"-"+month+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        });

        dp_1 = (DatePicker)findViewById(R.id.dp_1);

        dp_1.init(2015,0,1,new DatePicker.OnDateChangedListener(){

            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                Toast.makeText(TestActivity2.this, "选中的日期是:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        });

        tp_1 = (TimePicker)findViewById(R.id.tp_1);

        tp_1.setIs24HourView(true);

        tp_1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                Toast.makeText(TestActivity2.this, "选中的时间是:"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        });
    }



}
View Code

5.运行代码目录

技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.testapp2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TestActivity2">
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>

    </application>

</manifest>
View Code

 

日历 日期时间选择器

标签:

原文地址:http://www.cnblogs.com/bilibiliganbei/p/5478978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!