gpt4 book ai didi

android - 如何将日期选择器文本框日期限制为 future 日期。它应该只允许以前的日期

转载 作者:行者123 更新时间:2023-11-30 03:46:39 25 4
gpt4 key购买 nike

我想限制日期选择器将日期设置为高于系统当前日期。告诉我如何验证日期选择器日期。

最佳答案

Create Custom Dialog an show toat on date greater then current date


final Dialog dialog = new Dialog(SettingActivity.this);
dialog.setContentView(R.layout.date_picker_view);
dialog.setTitle("Select Date");
final TextView tvTime = (TextView) dialog
.findViewById(R.id.tv_time);
final DatePicker datePicker = (DatePicker) dialog
.findViewById`enter code here`(R.id.datePicker1);
Button set = (Button) dialog.findViewById(R.id.button_time_set);
Button cancel = (Button) dialog
.findViewById(R.id.button_time_cancel);

datePicker
.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

Calendar originalDate=Calendar.getInstance();
Calendar previousWeekDay = (Calendar) originalDate.clone();
previousWeekDay.add(Calendar.YEAR, -35);
previousWeekDay.set(Calendar.MONTH, 0);
previousWeekDay.set(Calendar.DAY_OF_MONTH, 1);

datePicker.init(previousWeekDay.get(Calendar.YEAR), previousWeekDay.get(Calendar.MONTH), previousWeekDay.get(Calendar.DAY_OF_MONTH), null);

final Calendar calendar = Calendar.getInstance();
final int mDay = calendar
.get(Calendar.DAY_OF_MONTH)+1;

calendar.set(Calendar.DAY_OF_MONTH, mDay);




set.setOnClickListener(new OnClickListener() {



public void onClick(View arg0) {
// TODO Auto-generated method stub

Calendar cal=Calendar.getInstance();
cal.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());

if(cal.after(calendar)){
Toast.makeText(SettingActivity.this, "DOB not more then current date", Toast.LENGTH_SHORT).show();
handler.etDOB.setText("");
}else{
date_selected = String.valueOf(datePicker.getDayOfMonth())+"/"+String.valueOf(datePicker.getMonth()+1)+"/"+String.valueOf(datePicker.getYear());

handler.etDOB.setText(datePicker.getDayOfMonth()+"-"+AccessRecordForPDF.MONTHS[datePicker.getMonth()+1]+"-"+String.valueOf(datePicker.getYear()));
dialog.dismiss();
}






}
});
cancel.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

dialog.show();


}
});


And create dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="270dp"
android:layout_height="190dp"
android:background="@android:color/black"
android:orientation="vertical" >


<DatePicker
android:id="@+id/datePicker1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />



<!--
<com.amvrin.PillsScheduler.DrumPicker.DateDrumPicker
android:id="@+id/datepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
-->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:orientation="horizontal" >

<Button
android:id="@+id/button_time_set"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_green"
android:text="Set"
android:textColor="@android:color/white" />

<Button
android:id="@+id/button_time_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_green"
android:text="Cancel"
android:textColor="@android:color/white" />
</LinearLayout>

</LinearLayout>

关于android - 如何将日期选择器文本框日期限制为 future 日期。它应该只允许以前的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14893703/

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