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

时间日期视图

时间:2016-05-11 11:03:07      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

MainActivity

技术分享
public class MainActivity extends AppCompatActivity {

    CalendarView cv_1;

    DatePicker dp_1;
    TimePicker tp_1;

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

        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(MainActivity.this, "选中的日期是:"+year+"-"+(month+1)+"-"+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(MainActivity.this, "选中的日期是:"+year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();

            }
        });

        tp_1=(TimePicker)findViewById(R.id.tp_1);
        tp_1.setIs24HourView(true);//24小时制
        tp_1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

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

            }
        });
    }
}
View Code

 

 

actuvity_main

技术分享
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lenovo.textapp4.MainActivity"
    android:orientation="vertical">

    <CalendarView
        android:layout_width="wrap_content"
        android:layout_height="284dp"
        android:firstDayOfWeek="1"
        android:maxDate="12/31/2016"
        android:minDate="01/01/2016"
        android:id="@+id/cv_1"
        android:visibility="gone">

    </CalendarView>

    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:startYear="2010"
        android:endYear="2018"
        android:maxDate="10/1/2017"
        android:minDate="02/01/2010"
        android:id="@+id/dp_1">

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

        </TimePicker>

    </DatePicker>

</LinearLayout>
View Code

 

时间日期视图

标签:

原文地址:http://www.cnblogs.com/1ming/p/5480919.html

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