我有一个DatePicker
,其中有一个按钮,可以让DialogBox选择日期,还有一个EditText txtDate
来设置日期:
Layout
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.50"
android:text="Pickup Date*"
android:textStyle="bold" />
<EditText
android:id="@+id/txtDatee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.28" >
</EditText>
<Button
android:id="@+id/btnCalendar"
android:layout_width="30dp"
android:layout_height="42dp"
android:layout_marginRight="50dp"
android:background="@drawable/date" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="62dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView5"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:text="Pickup Time"
android:textStyle="bold" />
<Spinner
android:id="@+id/hh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="@+id/mm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
的onClick [DatePicker
]:
public void onClick(View v) {
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
if (year < mYear)
view.updateDate(mYear,mMonth,mDay);
if (monthOfYear < mMonth && year == mYear)
view.updateDate(mYear,mMonth,mDay);
if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear,mMonth,mDay);
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
Date newDate2 = c.getTime();
dpd.getDatePicker().setMinDate(newDate2.getTime());
dpd.show();
} }
还有两个id为hh
和mm
的微调器,用于具有静态数组值的小时和分钟。
String[] pickup_hour = { "HH", "00", "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23" };
String[] pickup_min = { "MM", "00", "05", "10", "15", "20", "25", "30",
"35", "40", "45", "50", "55" };
sp3 = (Spinner) findViewById(R.id.hh);
sp3.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
edsp3.setText(pickup_hour[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter b3 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, pickup_hour);
b3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(b3);
sp4 = (Spinner) findViewById(R.id.mm);
sp4.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
edsp4.setText(pickup_min[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter b4 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, pickup_min);
b4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(b4);
根据当前日期,以前的日期禁用DatePicker
。
那么如何根据DatePicker
日期修复小时和分钟?
感谢您的帮助。
编辑:
这就是我在小时内所做的事情:
sp3 = (Spinner) findViewById(R.id.hh);
sp3.setOnItemSelectedListener(new OnItemSelectedListener() {
Calendar c1 = Calendar.getInstance();
int hour = c1.get(Calendar.HOUR_OF_DAY);
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
Log.d("aaa", ""+hour);
edsp3.setText(pickup_hour[position]);
if(position<hour)
{
Toast.makeText(OnlineBooking.this, "Choose greater hour",
Toast.LENGTH_LONG).show();
}}
答案 0 :(得分:0)
首先,始终建议您使用内置的 DatePicker 和 TimePicker 小部件。
如果您希望使用微调器,则以编程方式填充微调器
System.currentTimeMillis();
将为您提供设备的当前时间 然后检查当前的小时和分钟,并相应地填充微调器
其他方法是通过在提交按钮点击事件
中验证相同的方法表示使用当前系统时间检查所选时间
答案 1 :(得分:0)
根据时钟系统,24小时或12小时系统,获取
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR); // or HOUR_OF_DAY
当调用spinenrs onItemSelected
侦听器时,将这些值与您在秒,小时和分钟变量中的值进行比较,并相应地显示该消息。