Datepicker created with DialopFragment showing Cal

2019-06-12 04:09发布

问题:

I am new to Android programming and I am using a basic Datepicker created using a DialogFragment. My DatePicker shows up fine on click etc., but the problem is that it is displaying the Calendar view as well. I do not want this. I have been searching for a solution for a day now, and I am unwilling to use custom Datepickers right now, as I want to get familiar with the basics first.

I have also read suggestions such as

yourDatepicker.setCalendarViewShown(false); 

or set it to false in the XML. But my DatePicker comes from a DialogFragment, so how do I access this datepicker? How do I set its final view? I do not wish to make changes in the source code as I am still learning the ropes.

My datepicker code:

public class DueDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
            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);
            return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day){    
        // Code to set Due Date TextField to the selected date.
        TextView reminderText = (TextView)getActivity().findViewById(R.id.addTaskFormDueDateHolder);

        String dueDateStr = year + "-" + month + "-" + day;
        reminderText.setText(dueDateStr);
    }
}

回答1:

I have used Datepicker Dialog Fragment, to show the Date picker in dialog.

Here is my Complete Code

MainActivity.java

package com.example.testmydrag;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.DatePicker;
import android.support.v4.app.FragmentActivity;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;

import java.util.Calendar;

public class MainActivity extends FragmentActivity {

EditText mEdit;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void selectDate(View view) {

        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getSupportFragmentManager(), "DatePicker");

    }

    public void setTheDate(int year, int month, int day) {

        mEdit = (EditText)findViewById(R.id.Text);
        mEdit.setText(month+"/"+day+"/"+year);
    }
    /*
     * Date Picker Dialog
     */
    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar c = Calendar.getInstance();
            int yy = c.get(Calendar.YEAR);
            int mm = c.get(Calendar.MONTH);
            int dd = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,c.YEAR, c.MONTH, c.DATE);

            /*Calendar View if you want to Remove Set it to False*/
            dialog.getDatePicker().setCalendarViewShown(true);

            /*Spinner View if you want to Show Set it to True*/
            dialog.getDatePicker().setSpinnersShown(false);

            dialog.setTitle("Pick a date");
            return dialog;
        }
        public void onDateSet(DatePicker view, int yy, int mm, int dd) {

            setTheDate(yy, mm+1, dd);
        }
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText android:text="@+string/date_text" 
        android:id="@+id/Text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="@+string/pick_date" 
        android:onClick="selectDate" />

</LinearLayout>


回答2:

I found a temporary solution, though I am not entirely happy with it. You need minimum SDK version 11 for this.

Basically I added a call to getDatePicker() in my onAcreateDialog function, mentioned in the DueDatePickerFragment, and then changed its settings. I don't understand why I need to do this, and why it is appearing so incorrectly in my emulator. No one else seems to be having this problem. Any explanations would be highly appreciated. Here is the code:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    Log.i("DUEDATEPICKERDIALOG", "Inside onCreateDIALOG");
    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);
    DatePickerDialog datePickerDialogObj = new DatePickerDialog(getActivity(), this, year, month, day);

    DatePicker dueDatePicker = datePickerDialogObj.getDatePicker();
    float scaleVal = (float) 0.6;
    dueDatePicker.setScaleX(scaleVal);
    dueDatePicker.setScaleY(scaleVal);
    dueDatePicker.setCalendarViewShown(false);

    return datePickerDialogObj;
}

Hope this helps someone.