如何在Android Studio中正确创建日期选择器?

时间:2022-03-28 02:50:21

I'm following the instructions here: https://developer.android.com/guide/topics/ui/controls/pickers.html

我按照这里的说明操作:https://developer.android.com/guide/topics/ui/controls/pickers.html

I scrolled down to "Creating a Date Picker" and copy pasted this code in a file called DatePickerFragment.java:

我向下滚动到“创建日期选择器”并将此代码复制粘贴到名为DatePickerFragment.java的文件中:

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

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}

I then created this button in activity_front_page.xml:

然后我在activity_front_page.xml中创建了这个按钮:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pick_date"
    android:onClick="showDatePickerDialog" />

and added this code to the DatePickerFramegent class:

并将此代码添加到DatePickerFramegent类:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

But it is saying that getSupportFrammentManager() is not defined?

但是说没有定义getSupportFrammentManager()?

I'm new to using Android studio and developing for Andriod. What I want is the date picker (a picture of it is shown near the top of the screen if you access the link provided above). Any idea how to fix this issue?

我刚开始使用Android工作室并为Andriod开发。我想要的是日期选择器(如果您访问上面提供的链接,它的图片显示在屏幕顶部附近)。知道如何解决这个问题吗?

3 个解决方案

#1


0  

As you're using android.support.v4.app.DialogFragment, you should pass to show() an instance of android.support.v4.app.FragmentManager which can be queried using an getSupportFragmentManager() call

当您使用android.support.v4.app.DialogFragment时,您应该向show()传递android.support.v4.app.FragmentManager的一个实例,可以使用getSupportFragmentManager()调用查询该实例

#2


0  

You need FragmentTransaction not getSupportFragmentManager()

你需要FragmentTransaction而不是getSupportFragmentManager()

public void showDatePickerDialog(View v) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(ft, "datePicker");
}

#3


0  

Try the below code and see if it helps:

尝试下面的代码,看看它是否有帮助:

Activity activity = getActivity;
DatePickerDialog datePickerDialog = new DatePickerDialog(activity, AlertDialog.THEME_HOLO_LIGHT,
                new DatePickerDialog.OnDateSetListener() {

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

                }, year, mm, dd);
        datePickerDialog.show();

#1


0  

As you're using android.support.v4.app.DialogFragment, you should pass to show() an instance of android.support.v4.app.FragmentManager which can be queried using an getSupportFragmentManager() call

当您使用android.support.v4.app.DialogFragment时,您应该向show()传递android.support.v4.app.FragmentManager的一个实例,可以使用getSupportFragmentManager()调用查询该实例

#2


0  

You need FragmentTransaction not getSupportFragmentManager()

你需要FragmentTransaction而不是getSupportFragmentManager()

public void showDatePickerDialog(View v) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(ft, "datePicker");
}

#3


0  

Try the below code and see if it helps:

尝试下面的代码,看看它是否有帮助:

Activity activity = getActivity;
DatePickerDialog datePickerDialog = new DatePickerDialog(activity, AlertDialog.THEME_HOLO_LIGHT,
                new DatePickerDialog.OnDateSetListener() {

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

                }, year, mm, dd);
        datePickerDialog.show();