如何从我的DatePickerFragment传输日期字符串?

时间:2016-07-10 17:05:39

标签: java android

我从stackoverflow中的另一个问题中得到了这个来源..

这是我的fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#e1e1e1">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="55dp"
        android:scrollbars="vertical">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="10dp">


            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="#fff"
                android:padding="10dp"
                android:layout_marginBottom="10dp">


                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:background="#fff">

                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="Set Reminder"
                        style="@style/Base.TextAppearance.AppCompat.Medium"
                        android:layout_marginBottom="5dp"
                        android:layout_marginTop="10dp"
                        android:textColor="#009cee"/>

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:background="#e1e1e1"
                        android:layout_marginBottom="25dp"/>


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Range Hari"
                        android:textStyle="bold"
                        android:textSize="16sp"/>


                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="5dp"
                        android:inputType="number"
                        android:gravity="top"
                        android:textSize="16sp"
                        android:id="@+id/range"
                        android:ems="10" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Tanggal Awal"
                        android:textStyle="bold"
                        android:textSize="16sp"/>

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:orientation="horizontal"
                        android:background="#fff">

                    <EditText
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:paddingTop="5dp"
                        android:gravity="top"
                        android:textSize="16sp"
                        android:id="@+id/tanggalawaltxt"
                        android:ems="10" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:background="#EC407A"
                        android:textColor="#ffffff"
                        android:text="Set Tanggal"
                        android:textSize="14sp"
                        android:id="@+id/tanggal"
                        android:layout_marginBottom="15dp"/>

                    </LinearLayout>


                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:orientation="horizontal"
                        android:layout_marginTop="20dp"
                        android:background="#fff">


                        <Button
                            android:layout_width="match_parent"
                            android:layout_marginLeft="10dp"
                            android:layout_height="wrap_content"
                            android:background="#009cee"
                            android:textColor="#ffffff"
                            android:text="Simpan"
                            android:textSize="14sp"
                            android:id="@+id/simpan"
                            android:layout_marginBottom="15dp"/>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>



        </LinearLayout>

    </ScrollView>

</LinearLayout>

这是我的DatePickerFragment.java

package com.example.gandi.symanlub;

/**
 * Created by mgd on 10/07/16.
 */



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

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
    }
}

这是我的setreminder.java

package com.example.gandi.symanlub;


public class Setreminder extends Fragment {


    Button btnshowtanggal;
    TextView tanggalawal;
    View rootview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootview = inflater.inflate(R.layout.fragment_setreminder, container, false);

        btnshowtanggal = (Button)rootview.findViewById(R.id.tanggal);
        btnshowtanggal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment picker = new DatePickerFragment();
                picker.show(getActivity().getFragmentManager(), "datePicker");

                tanggalawal = (TextView) rootview.findViewById(R.id.tanggalawaltxt);

            }
        });

        return rootview;

    }



}

如何将Datetimepicker所选日期的字符串输入tanggalawal(TextView)?

1 个答案:

答案 0 :(得分:0)

1)在SetReminder中创建一个名为DateListener的接口,其方法为onDateSelected(Date date);

2)使用SetReminder Fragment创建任何Activity来实现接口SetReminder.DateListener。

3)在SetReminder Fragment中的onAttach(活动活动)中,检查附加的Activity是否实现了SetReminder.DateListener,如果是,则将Fragment的DateListener初始化。

4)通常你想要扩展你的流程,并且可能在你实际上在你的监听器上调用onDateSet之前添加一个AlertDialog来添加一个确认按钮。在你的情况下,你可以调用onDateSelected(Date date),通过调用

为你的活动添加一个委托回调
if (mDateListener != null){
   mDateListener.onDateSelected(calendar.getTime());
}

编辑:对于Dialog代码示例...您应该能够弄清楚如何在Activity中实现提供的接口,并将其设置在正在侦听日期更新的视图上。

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

public interface DateTimePickedListener {
        void onDateTimePicked(String formattedDate);
    }

    DateTimePickedListener listener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof DateTimePickedListener){
            listener = (DateTimePickedListener) activity;
        }
    }


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

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
        if(listener != null){
            listener.onDateSelected(formattedDate);
        }
    }
}