Step by step learning Android control (twelve) -- DatePicker

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
DatePicker -The date selector, use the control to ensure that the user selected date is valid. DatePicker also adaptive user language environment (user habit).
DatePicker has a date change listener OnDateChangedListener, every date changes after the notification to the onDateChanged function, make the corresponding operation within the function.
Following a brief example: display the following interface date does not change:


When the user modification date, display the following interface


The interface has a "Date Changed in DatePicker" notification message.
Relatively simple for the control of learning, look at the layout file widget_datepicker_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="104dp" />

</RelativeLayout>
The paper shows that the use of a DatePicker control.
Then use the control in the WidgetDatepickerActivity.java, WidgetDatepickerActivity contents are as follows:

package com.xy.zt.selfdefinewieget;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;

public class WidgetDatepickerActivity extends Activity implements
        DatePicker.OnDateChangedListener {
    private DatePicker mShowDate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.widget_datepicker_layout);
        init();
    }

    private void init() {
        mShowDate = (DatePicker) findViewById(R.id.datePicker1);
        Calendar c = Calendar.getInstance();
        mShowDate.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH), this);
        mShowDate.getCalendarView();
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        Toast.makeText(this, "Date Changed in DatePicker;", Toast.LENGTH_LONG)
                .show();

    }

}
valueNote that the listener is arranged in the DatePicker.init function, does not provide setXXXListener method.
The following code is used to improve the whole project:
Add the following content in ViewData.java:

public static final int DATE_PICKER_ID = CHRONOMETOR_ID + 1;
    public static final String DATE_PICKER_NAME = "DatePicker";
private static final ViewData mDatePicker = new ViewData(DATE_PICKER_NAME,
            DATE_PICKER_ID);
View_Datas.add(mDatePicker);

Add the following content in the handleItemClicked method in WidgetsAdapter.java:

case ViewData.DATE_PICKER_ID:
            intent.setClass(mContext, WidgetDatepickerActivity.class);
            mContext.startActivity(intent);
            break;
The above content if have not clear place, see step by step learning Android control (one) -- began, DatePicker controls to learn here, a control TimePicker.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Shawn at December 10, 2013 - 2:51 AM