gpt4 book ai didi

android - Datepicker:如何在单击按钮时弹出日期选择器并将值存储在 textview 中

转载 作者:行者123 更新时间:2023-11-29 20:06:22 25 4
gpt4 key购买 nike

 <TextView
android:id="@+id/tv_arrival"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/arrivaldate"/>

<Button
android:id="@+id/btn_arrival"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Set"/>

我想显示日期选择器弹出窗口。我找到了一些例子,但我没有正确理解。我有一个按钮,我希望当我点击按钮时,日期选择器对话框应该弹出,并且在设置日期之后,日期应该显示在一个 .请提供示例代码或好的链接。

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

final Dialog setDate = new Dialog(MainActivity.this);
setDate.requestWindowFeature(Window.FEATURE_NO_TITLE);
setDate.setContentView(R.layout.dialog_setdate);
setDate.show();
DatePicker date = (DatePicker) setDate.findViewById(R.id.datePicker);
date.setMinDate(System.currentTimeMillis() - 1000);
Calendar calender = Calendar.getInstance();

DatePicker.OnDateChangedListener onDateChangedListener = new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int years, int monthOfYear, int dayOfMonth) {
day = dayOfMonth;
year = years;
month = monthOfYear;
setDate.dismiss();
txt_arrival.setText("Arrival Date on: " + day + "." + (month + 1) + "." + year);
tv_departure.setText("Departure Date on: " + (day + 1) + "." + (month + 1) + "." + year + "");
}
};

day = calender.get(Calendar.DAY_OF_MONTH);
month = calender.get(Calendar.MONTH);
year = calender.get(Calendar.YEAR);

date.init(year, month, day, onDateChangedListener);

}

});

最佳答案

这是示例代码像这样在您的 View 上设置 onclicklistener

 Calendar dateSelected = Calendar.getInstance();
private DatePickerDialog datePickerDialog;
dateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setDateTimeField() ;
}
});


private void setDateTimeField() {
Calendar newCalendar = dateSelected;
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateSelected.set(year, monthOfYear, dayOfMonth, 0, 0);
dateEditText.setText(dateFormatter.format(dateSelected.getTime()));
}

}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
dateEditText.setText(dateFormatter.format(dateSelected.getTime()));
}

或者您可以点击此链接

Datepicker: How to popup datepicker when click on edittext

关于android - Datepicker:如何在单击按钮时弹出日期选择器并将值存储在 textview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com