如何在自定义对话框中使用datepickerdialog

时间:2016-04-25 23:49:30

标签: android datepicker dialog listener customdialog

当用户点击对话框中的editext时,我想使用datepickerdialo。 我做了像这样的自定义对话

enter image description here

我需要在点击editext后显示datepickerdialo

这里是自定义对话框XML代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="12dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="12dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dialog_start_time"
            android:id="@+id/dialog_start_date"
            android:layout_marginTop="7dp"
            android:textSize="18dp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:id="@+id/dpStartDate"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="12dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="12dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dialog_end_time"
            android:id="@+id/dialog_end_date"
            android:textSize="18dp"

            />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:id="@+id/dpEndDate"/>

    </LinearLayout>

</LinearLayout>

这里是DialogFragment

public class CalcDialog extends DialogFragment{
final Calendar calendar = Calendar.getInstance();
final int startYear = calendar.get(Calendar.YEAR);
final int startMonth = calendar.get(Calendar.MONTH);
final int startDay = calendar.get(Calendar.DAY_OF_YEAR);

public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Dialog dialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.custom_calc_dialog, null);
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.custom_calc_dialog, null))
            // Add action buttons

            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    CalcDialog.this.getDialog().cancel();
                }
            }).setTitle("Period");
    dialog = builder.create();
    EditText startDateEditText = (EditText) view.findViewById(R.id.dpStartDate);
    startDateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(dialog.getContext(), listener1, startYear, startMonth, startDay).show();
        }
    });


    return dialog;

}
DatePickerDialog.OnDateSetListener listener1 = new DatePickerDialog.OnDateSetListener(){

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

    }
};
}

我初学者请回答清楚

1 个答案:

答案 0 :(得分:-1)

您需要更改

builder.setView(inflater.inflate(R.layout.custom_calc_dialog, null))

builder.setView(view)