DatePickerDialog日期使用

package com.example.winter.test;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;

import java.util.Calendar;

/** * Created by WINTER on 2015/9/13. */
public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
   private DatePickerDialogTest  acty;

    /*因为FragMent 要依赖于一个Activity so 强制转换 Activity*/
    /*创建日起对话框*/

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        acty= (DatePickerDialogTest) getActivity();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
        return dialog;
        //获取当前系统时间


    }

    @Override
    public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        acty.setTimeValue(year,monthOfYear,dayOfMonth);

    }
}
package com.example.winter.test;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/** * Created by WINTER on 2015/9/13. */
public class DatePickerDialogTest extends Activity {
    private Button tww1;

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

        tww1 = (Button) findViewById(R.id.t21);
    }
    public void getDate(int year, int monthOfYear, int dayOfMonth) {
        tww1.setText(year);
       /* Toast.makeText(DatePickerDialogTest.this,year + monthOfYear + dayOfMonth,Toast.LENGTH_LONG).show();*/
    }
    public void onclick(View v) {
        DialogFragment picker  =new DatePicker();
        picker.show(getFragmentManager(), "tag");

    }

    public void  setTimeValue(int year, int monthOfYear , int dayOfMonth){
     tww1.setText(year+"-"+monthOfYear+"-"+dayOfMonth);

    }
}


代码区(http://download.csdn.net/detail/bxllove/9103731)

你可能感兴趣的:(DatePickerDialog日期使用)