日付ピッカーを制限して、システムの現在の日付より上の日付を設定したいと考えています。デートピッカーの日付を検証する方法を教えてください。
質問する
1670 次
4 に答える
1
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>
于 2013-02-15T12:51:58.527 に答える
0
package com.example.pickerdate;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView mDateDisplay;
private int mYear;
private int year1;
private int mMonth;
private int mDay;
private int month;
private int day;
static final int DATE_DIALOG_ID = 1;
Button pickDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
pickDate = (Button) findViewById(R.id.pickDate);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("hello3");
showDialog(DATE_DIALOG_ID);
}
});
System.out.println("hello1");
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
year1 = mYear;
month = mMonth;
day = mDay;
}
@Override
protected Dialog onCreateDialog(int id) {
DatePickerDialog _date = null;
switch (id) {
case DATE_DIALOG_ID:
_date = new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay) {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
System.out.println("----------onDateChanged()-----------"
+ mYear + "--" + year);
System.out.println("----------onDateChanged()-----------"
+ mMonth + "--" + monthOfYear);
System.out.println("----------onDateChanged()-----------"
+ mDay + "--" + dayOfMonth);
/*
* These lines of commented code used for only setting the
* maximum date on Date Picker..
*
* if (year > mYear && year) 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);
*/
// these below lines of code used for setting the maximum as
// well as minimum dates on Date Picker Dialog..
if ((mYear > year)
|| ((mMonth > monthOfYear) && (mYear == year))
|| ((mDay > dayOfMonth) && (mYear == year) && (mMonth == monthOfYear))) {
view.updateDate(year1, month, day);
}
}
};
}
System.out.println("hello5");
return _date;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("hello6");
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplay() {
System.out.println("hello2");
mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
System.out.println("hello7");
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
System.out.println("year=" + year);
System.out.println("month=" + monthOfYear);
System.out.println("day=" + dayOfMonth);
updateDisplay();
}
};
}
于 2013-07-26T05:54:12.557 に答える
0
public Dialog oncreateDialogue(int id)
{
Calendar currentDate= Calendar.getInstance();
final int cyear = currentDate.get(Calendar.YEAR);
final int cmonth = currentDate.get(Calendar.MONTH);
final int cday = currentDate.get(Calendar.DAY_OF_MONTH);
// Log.d("", "cyear"+cyear + cmonth + cday);
DatePickerDialog dialog = null;
/* if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
{
dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
{
// mTextView.setText(selectedDate + "/" + selectedMonth + 1 + "/" + selectedYear);
Log.d("", "ondateset"+selectedDate + "/" + selectedMonth + "/" + selectedYear);
}
}, cyear, cmonth, cday);
// Calendar currentDate = Calendar.getInstance();
dialog.getDatePicker()
//If you need you can set min date too
}
else
{*/
dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDate)
{
int lmn=selectedMonth+1;
sp_dobFlag=true;
selectedDatepublic=selectedDate + "/"+lmn + "/"+selectedYear ;
ed_dob.setText(selectedDatepublic);
//Log.d("", "ondateset"+selectmnth);
}
}, cyear, cmonth , cday)
{
@Override
public void onDateChanged(DatePicker view, int year, int month, int day)
{
Log.d("", "not>--ondatechange"+cday);
Log.d("", "not>--ondatechange"+day);
if (year > cyear)
{
Log.d("", "date >cdate");
view.updateDate(cyear, cmonth, cday); //update the date picker to selected date
}
if(year==cyear)
{
if(month > cmonth)
{
view.updateDate(cyear, cmonth, cday);
}
}
if(year==cyear)
{
if(month == cmonth)
{
if(day > cday)
{
view.updateDate(cyear, cmonth, cday);
}
}
}
}
};
// }
return dialog;
}
于 2013-10-30T11:21:56.987 に答える
0
API レベル 11 以降、そのためのメソッドがあります。
1.DatePicker.setMaxDate(長い最大日付)
2.以前のバージョンで動作する必要がある場合は、次の方法を使用します。
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
于 2013-07-26T06:08:55.370 に答える