Created
September 12, 2013 07:11
-
-
Save daichan4649/6533878 to your computer and use it in GitHub Desktop.
DatePicker sample (Android)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="16dip" > | |
<DatePicker | |
android:id="@+id/datePicker" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:calendarViewShown="false" | |
android:spinnersShown="true" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SelectDateDialogFragment extends DialogFragment { | |
public static DialogFragment newInstance() { | |
Bundle args = new Bundle(); | |
DialogFragment fragment = new SelectDateDialogFragment(); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
// 表示日付 | |
Date initDate = new Date(); | |
AlertDialog.Builder builder = new Builder(getActivity()); | |
builder.setView(createView(initDate)); | |
builder.setPositiveButton("選択", new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
okProcess(); | |
} | |
}); | |
builder.setNegativeButton("キャンセル", new OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
cancelProcess(); | |
} | |
}); | |
return builder.create(); | |
} | |
private DatePicker mDatePicker; | |
private View createView(final Date date) { | |
View view = getActivity().getLayoutInflater().inflate(R.layout.inflate_dialog_select_date, null, false); | |
// 日付設定 | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
int year = cal.get(Calendar.YEAR); | |
int month = cal.get(Calendar.MONTH); | |
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); | |
mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); | |
mDatePicker.updateDate(year, month, dayOfMonth); | |
return view; | |
} | |
private void okProcess() { | |
if (mDatePicker == null) { | |
return; | |
} | |
int year = mDatePicker.getYear(); | |
int month = mDatePicker.getMonth(); | |
int dayOfMonth = mDatePicker.getDayOfMonth(); | |
String selectedDate = String.format("%d/%d/%d", year, month + 1, dayOfMonth); | |
CareSupportUtil.showToast(getActivity(), selectedDate); | |
} | |
private void cancelProcess() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment