【发布时间】: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