0

我是 Android 编程新手,我正在使用使用 DialogFragment 创建的基本 Datepicker。我的 DatePicker 在点击等时显示良好,但问题是它也显示日历视图。我不想要这个。我一直在寻找一天的解决方案,我现在不愿意使用自定义 Datepickers,因为我想先熟悉基础知识。

我还阅读了一些建议,例如

yourDatepicker.setCalendarViewShown(false); 

或在 XML 中将其设置为 false。但是我的 DatePicker 来自一个 DialogFragment,那么如何访问这个 datepicker?如何设置其最终视图?我不希望对源代码进行更改,因为我仍在学习。

我的日期选择器代码:

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

2 回答 2

1

我使用了 Datepicker Dialog Fragment,在对话框中显示日期选择器。

这是我的完整代码

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

}

主要的.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>
于 2013-02-04T17:00:59.887 回答
0

我找到了一个临时解决方案,尽管我对此并不完全满意。为此,您需要最低 SDK 版本 11。

基本上我在我的 onAcreateDialog 函数中添加了对 getDatePicker() 的调用,在 DueDatePickerFragment 中提到,然后更改了它的设置。我不明白为什么我需要这样做,以及为什么它在我的模拟器中显示得如此不正确。似乎没有其他人有这个问题。任何解释将不胜感激。这是代码:

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

希望这可以帮助某人。

于 2013-02-01T06:56:54.273 回答