将数据从DatePickerDialog获取到片段

时间:2014-01-29 21:33:44

标签: android android-fragments datepickerdialog

如上所述,我试图找出如何传回用户选择的日期。我已经研究了如何使用onDateSet方法获取日期,但我不知道如何将其反馈给父片段,然后将EditText文本设置为所选日期。

package com.example.androidvehicle;

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.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class fragmentServicing extends Fragment  {


    callBack mCallBack;

    Button mot;
    Button servicing;
    Button tax;
    EditText txtService;

    final int Date_Dialog_ID=0;
    int cDay,cMonth,cYear; // this is the instances of the current date
    Calendar cDate;
    int sDay,sMonth,sYear; // this is the instances of the entered date

    int id_dialog = 1;
    int yr, day, month = 0;

    //  interfacing back to activity
    public interface callBack
    {
        public void onItemSelected(String id);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_servicing, container, false);
        txtService = (EditText) view.findViewById(R.id.txtServiceDate);
        mot = (Button) view.findViewById(R.id.buttonMOT);
        servicing = (Button) view.findViewById(R.id.buttonService);
        servicing.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //  this will then pass back to the activity the string hello
                mCallBack.onItemSelected("hello");

                getActivity().showDialog(1);

            }

        });

        mot.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getActivity().showDialog(1);



            }

        });

        return view;
    }



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallBack = (callBack) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }



//  this is an override for when the dialog is created
    protected Dialog onCreateDialog(int id)
    {
        switch(id)
        {
        //  this will return a date picker dialog if 1 is passed
        //  could use another number for another dialog
        case 1:
            //  passes it the current date

            return new DatePickerDialog(getActivity(), mDateSetListener, yr, month, day);
        }
        return null;
    }
    //  this returns the date back that has been selected by the user

    private DatePickerDialog.OnDateSetListener mDateSetListener = 
            new DatePickerDialog.OnDateSetListener() {

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

                    yr = year;
                    month = monthOfYear;
                    day = dayOfMonth;

                    Log.d("date selected", "year "+ yr+  " month " +month+" day "+day);


                }


            };

}

1 个答案:

答案 0 :(得分:0)

您可以在Edittext onClicklistener上设置DateListner ..

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class DateListener implements OnClickListener, OnDateSetListener {

    private Activity activity;

    private int year;
    private int monthOfYear;
    private int dayOfMonth;

    private View touchedView;

    public DateListener(Activity activity) {
        this.activity = activity;
        final Calendar c = Calendar.getInstance();
        this.year = c.get(Calendar.YEAR);
        this.monthOfYear = c.get(Calendar.MONTH);
        this.dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
    }

    public int getYear() {
        return year;
    }
    public int getMonth() {
        return monthOfYear;
    }
    public int getDay() {
        return dayOfMonth;
    }

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        this.year = year;
        this.monthOfYear = monthOfYear + 1;
        this.dayOfMonth = dayOfMonth;

        updateDisplay();
        updateEditText();
    }

    @Override
    public void onClick(View v) {
        touchedView = v;

        new DatePickerDialog(activity,
                this, this.getYear(), this.getMonth(), this.getDay()).show();
    }

    private void updateDisplay() {
        ((TextView) touchedView).setText(
            new StringBuilder()
                    .append(pad(dayOfMonth)).append(".")
                    .append(pad(monthOfYear)).append(".")
                    .append(pad(year)));
    }

    private void updateEditText() {
        ((EditText) touchedView).setText(
            new StringBuilder()
                    .append(pad(dayOfMonth)).append(".")
                    .append(pad(monthOfYear)).append(".")
                    .append(pad(year)));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

}

在你的片段中

DateListener dateListener = new DateListener(getActivity());

edittext.setOnClickListener(dateListener);