下面是我的代码,可以在点击edittext后弹出日期对话框。我的主要问题是限制用户选择过去的日期作为输入。
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" />
非常感谢您的反馈和帮助。
答案 0 :(得分:2)
使用DatePicker类的setMinDate方法。
datePicker.setMinDate(System.currentTimeMillis() - 1000);
答案 1 :(得分:0)
首先选择当前日期
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet">
<div class="row speakers_list p-t-md p-l-md" >
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 m-b-lg">
<div class="selected_div">
<i class="fa fa-check text-white"></i>
</div>
<div class="speaker_div">
<img src="http://img.xcitefun.net/users/2011/02/231256,xcitefun-beautiful-small-nature-world-01.jpg" class=" img-responsive img-circle">
</div>
<div class="speaker_name text-center m-t-xs">
<span> 1 </span>
</div>
</div>
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 m-b-lg">
<div class="speaker_div">
<img src="http://img.xcitefun.net/users/2011/02/231256,xcitefun-beautiful-small-nature-world-01.jpg" class=" img-responsive img-circle">
</div>
<div class="speaker_name text-center m-t-xs">
<span> 2 </span>
</div>
</div>
</div>
现在将日期与所选日期进行比较
Calendar c = Calendar.getInstance();
Date currentDate= c.getTime();
答案 2 :(得分:0)
使用此
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,-1);
dialog.getDatePicker().setMaxDate(calendar.getTime().getTime());
这是限制昨天日期的最佳方法。