从DatePickerDialog获取数据到片段 [英] Getting data from a DatePickerDialog to a fragment

查看:93
本文介绍了从DatePickerDialog获取数据到片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As above I am trying to work out how to pass back the date selected by the user. I have worked out how to get the date selected by using the onDateSet method but I do not know how to feed this back to the parent fragment, and then set the EditText text to the date selected.

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);


                }


            };

}


推荐答案

您可以

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);

edittext.setOnClickListener(dateListener);

这篇关于从DatePickerDialog获取数据到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆