我正在寻找解决问题的方法。我想在活动中创建按钮,然后单击以显示日期选择器。如何在databindind中执行此操作?有绑定适配器吗?
答案 0 :(得分:1)
private void openDatePicker() {
final Calendar cal = Calendar.getInstance();
int day;
int month;
int year;
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
cal.set(year, month, day);
final DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), new DatePickerDialog.OnDateSetListener() {
@SuppressLint("SimpleDateFormat")
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar choosen = Calendar.getInstance();
choosen.set(view.getYear(), view.getMonth(), view.getDayOfMonth());
Date date = choosen.getTime();
// Do whatever you want
}
}, year, month, day);
datePickerDialog.setTitle("Select Date");
datePickerDialog.setCanceledOnTouchOutside(true);
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", datePickerDialog);
datePickerDialog.show();
}
数据绑定:
Button button = findViewById(R.id.your_button_id);
button.setOnClickListener(new View.OnClickListener(){
void onClick(View view){
openDatePicker();
}
});
答案 1 :(得分:1)
初始化DatePickerDialog
private DatePickerDialog.OnDateSetListener date;
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
/////// Here You will get Selected Date from Calendar ///////
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
button.setText(dateFormat.format(cal.getTime()));
}
};
在“按钮单击”上编写此代码
new DatePickerDialog(getActivity(), R.style.MyDialogTheme, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
答案 2 :(得分:1)
这对我有用,我正在我的代码中使用它。我正在活动本身中创建此类。
DATE_DIFF(Begindt, CURRENT_DATE, day)
用法:
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private Calendar calendar;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
{
// Use the current date as the default date in the picker
calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
if (getActivity() != null)
{
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
return super.onCreateDialog(savedInstanceState);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
calendar.set(year, month, dayOfMonth);
// You may use different date format according to your use..
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
String selectedDate = sdf.format(calendar.getTime()); // this is the date which the user will select
}
}