自訂一個 class extends DialogFragment
public class MyDialogFragment extends DialogFragment implements OnDateSetListener{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
String mYear = String.valueOf(year);
String mMonth = (monthOfYear+1 > 9)? (monthOfYear+1) +"" : "0" + (monthOfYear+1);
String mDay = (dayOfMonth > 9)? (dayOfMonth) +"" : "0" + (dayOfMonth);
EditText etday = (EditText)getActivity().findViewById(R.id.et_birthday);
etday.setText(mYear + mMonth + mDay);
}
@Override
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);
}
}
在要顯示Dialog 的 Activity 使用
MyDialogFragment myDateDialog = new MyDialogFragment();
myDateDialog.show(getFragmentManager(), "datePicker");
即可...
紅色部分為 layout 中的 EditText
沒有留言 :
張貼留言