android中的DatePicker示例

时间:2010-12-01 04:14:06

标签: android datepicker

请给我一些教程,它给出了DatePicker的示例以及如何使用它的方法,如OnDateChangedListener,onDateChanged等。实际上我正在浏览一些网站,但我没有清楚地了解它。

谢谢

5 个答案:

答案 0 :(得分:9)

DatePicker上的Android引用非常好。看看它here

private DatePicker datePicker;
//monthofYear is between 0-11
datePicker.init(2010, 11, 1, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
    // Notify the user.

}
});

答案 1 :(得分:2)

请参阅此处Example();

答案 2 :(得分:0)

检查此数据选择器示例:Example of DATE PICKER

答案 3 :(得分:0)

Step 1 : create a java file:

package com.example.babs;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import  android.app.FragmentManager;


public class EditUserInfo extends Activity {


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

    }

    public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {


        // pgrm mark ---- ---- ----- ---- ---- ----- ---- ---- -----  ---- ---- -----

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
        }

    }


    public void showDatePickerDialog(View v) {

        FragmentManager fragmentManager = getFragmentManager();

        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(fragmentManager, "datePicker");
    }

}// end main class EditUserInfo

step 2: your xml file must contain : 

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:fillViewport="true" >
</ScrollView>
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@string/pick_date" 
    android:onClick="showDatePickerDialog" />

答案 4 :(得分:0)

您可以尝试以下代码:

public
static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit.setText(day + "/" + (month + 1) + "/" + year);
}
}

取自Example of DatePickerFragment and TimePickerFragment