如何通过按下按钮调用我的日期选择器?

How do I call my Date Picker from a button press?

我正在尝试创建一个允许我选择日期的应用程序,table 填充了当天的约会。我无法显示日期选择器。

我已经查看了这个 https://developer.android.com/guide/topics/ui/controls/pickers 并从该代码创建了一个日期选择器。我想通过 AddAppointmentActivity class.

中的按钮调用该日期选择器

这是我的约会 class:

package com.example.dentdevils.ui.HomeLinks;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.example.dentdevils.R;
import com.example.dentdevils.ui.HomeActivity;


import java.util.Calendar;

public class AddAppointmentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_appointment);

        // Date label
        TextView dateLabel = (TextView) findViewById(R.id.txtDateLabel);

        // Date Picker Button Functionality
        Button datePicker = (Button) findViewById(R.id.btnDatePicker);
        datePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        // Back Button Functionality
        Button btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent launchHomepage = new Intent(AddAppointmentActivity.this, HomeActivity.class);
                startActivity(launchHomepage);
            }
        });
    }

}

class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        // Get date set by user and display appointments in table for the date chosen.

    }

    public void showDatePickerDialog() {
        DialogFragment newFragment = new com.example.dentdevils.ui.HomeLinks.DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

}

我确实在不同的文件中有 classes 但测试的是不同的东西因此它们在同一个文件中的原因(我会在我设法调用日期后再次将它们移回单独的文件选择器)。

我的问题是,如何通过按下按钮调用日期选择器对话框?

datePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

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


        }
    });

您不需要为日期选择器创建额外的对话框片段。只需创建 DatePickerDialog 对象并调用 show().

示例:

val calendar = Calendar.getInstance()

val dialog = DatePickerDialog(context,
                    DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                        // TODO
                    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH))

dialog.show()