如何限制用户在日期选择器对话框中选择过去的日期
How to restrict a user from selecting past dates in a datepicker dialog
下面是我的代码,可以在单击编辑文本后弹出日期对话框。我的主要问题是限制用户选择过去的日期作为输入。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner_from = (Spinner) findViewById(R.id.spinFrom);
final Spinner spinner_to = (Spinner) findViewById(R.id.spinTo);
Button Searchbtn = (Button) findViewById(R.id.btn_search);
Searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String location = spinner_from.getSelectedItem().toString();
String destination = spinner_to.getSelectedItem().toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//adds icons to the action bar
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//to handle actions performed by icons on action bar
switch (item.getItemId()){
case R.id.admin_button:
startActivity(new Intent(this, AdminLogin.class));
return true;
case R.id.action_about:
startActivity(new Intent(this, AboutBookBus.class));
return true;
}
return super.onOptionsItemSelected(item);
}
//to handle the date dialog and set it to the edittext
public void onStart(){
super.onStart();
EditText travelDate = (EditText)findViewById(R.id.travelDate);
travelDate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
DateDialog dialog = new DateDialog(view);
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
});
}
}
DateDialog.java
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText travelDate;
String Current_date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
public DateDialog(View view) {
travelDate = (EditText) view;
}
//dialog context to set the date
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
String date = year + "-" + (month + 1) + "-" + day;
travelDate.setText(date);
}
}
activity_main.xml
中的 Edittext 部分
<EditText
android:layout_width="350dp"
android:layout_height="42dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/travelDate"
tools:ignore="HardcodedText,TextFields"
android:hint="Touch to add travel date"
app:layout_constraintLeft_toLeftOf="@+id/textView"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintRight_toRightOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.75" />
非常感谢您的反馈和帮助。
使用 DatePicker 的 setMinDate 方法class。
datePicker.setMinDate(System.currentTimeMillis() - 1000);
第一个 select 当前日期
Calendar c = Calendar.getInstance();
Date currentDate= c.getTime();
现在将日期与select编辑的日期
进行比较
if (selectedDate.after(currentDate)) {
// Do something here
} else {
// Print here "Please select date after current date"
}
用过这个
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,-1);
dialog.getDatePicker().setMaxDate(calendar.getTime().getTime());
这是限制昨天日期的最佳方式。
下面是我的代码,可以在单击编辑文本后弹出日期对话框。我的主要问题是限制用户选择过去的日期作为输入。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner_from = (Spinner) findViewById(R.id.spinFrom);
final Spinner spinner_to = (Spinner) findViewById(R.id.spinTo);
Button Searchbtn = (Button) findViewById(R.id.btn_search);
Searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String location = spinner_from.getSelectedItem().toString();
String destination = spinner_to.getSelectedItem().toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//adds icons to the action bar
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//to handle actions performed by icons on action bar
switch (item.getItemId()){
case R.id.admin_button:
startActivity(new Intent(this, AdminLogin.class));
return true;
case R.id.action_about:
startActivity(new Intent(this, AboutBookBus.class));
return true;
}
return super.onOptionsItemSelected(item);
}
//to handle the date dialog and set it to the edittext
public void onStart(){
super.onStart();
EditText travelDate = (EditText)findViewById(R.id.travelDate);
travelDate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
DateDialog dialog = new DateDialog(view);
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
});
}
}
DateDialog.java
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText travelDate;
String Current_date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
public DateDialog(View view) {
travelDate = (EditText) view;
}
//dialog context to set the date
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
String date = year + "-" + (month + 1) + "-" + day;
travelDate.setText(date);
}
}
activity_main.xml
中的 Edittext 部分<EditText
android:layout_width="350dp"
android:layout_height="42dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/travelDate"
tools:ignore="HardcodedText,TextFields"
android:hint="Touch to add travel date"
app:layout_constraintLeft_toLeftOf="@+id/textView"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintRight_toRightOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.75" />
非常感谢您的反馈和帮助。
使用 DatePicker 的 setMinDate 方法class。
datePicker.setMinDate(System.currentTimeMillis() - 1000);
第一个 select 当前日期
Calendar c = Calendar.getInstance();
Date currentDate= c.getTime();
现在将日期与select编辑的日期
进行比较if (selectedDate.after(currentDate)) {
// Do something here
} else {
// Print here "Please select date after current date"
}
用过这个
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,-1);
dialog.getDatePicker().setMaxDate(calendar.getTime().getTime());
这是限制昨天日期的最佳方式。