【问题标题】:How do I call my Date Picker from a button press?如何通过按下按钮调用我的日期选择器?
【发布时间】:2020-06-24 10:20:22
【问题描述】:

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

我查看了这个https://developer.android.com/guide/topics/ui/controls/pickers 并从这个代码创建了一个日期选择器。我想通过AddAppointmentActivity 类中的按钮调用该日期选择器。

这是我的约会课程:

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

}

我确实将这些类放在单独的文件中,但正在测试不同的东西,因此为什么它们在同一个文件中(在我设法调用日期选择器后,我会将它们移回单独的文件中)。

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

【问题讨论】:

标签: android android-datepicker


【解决方案1】:
datePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

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


        }
    });

【讨论】:

    【解决方案2】:

    您不需要为日期选择器创建额外的对话框片段。只需创建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()
    

    【讨论】:

      猜你喜欢
      • 2011-08-01
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2019-06-30
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多