Como criair checkboxs de dialog DatePicker e timePicker na class de fragments?

Quero saber se existe uma maneira de criair uma dataPicker em um fragment? Estou criando uma atividade regulair e pode me dair erro de syntax. Qual é a maneira correta de fazer isso?

5 Solutions collect form web for “Como criair checkboxs de dialog DatePicker e timePicker na class de fragments?”

Você provavelmente precisairá usair um DialogFragment. Encontrei algumas informações aqui:

Mostrair dialog a pairtir do fragment?

e também uma grande ajuda aqui:

https://github.com/commonsguy/cw-advandroid/blob/master/Honeycomb/FeedFragments/src/com/commonswaire/android/feedfrags/AddFeedDialogFragment.java

Isso deve ajudair você a seguir seu path, agora estou fazendo isso mesmo. Embora dentro do código de exemplo eu não use um construtor e, em vez disso, simplesmente retornair:

return new DatePickerDialog(getActivity(), mDateSetListener, mYeair, mMonth, mDay); 

Isso pairece funcionair … embora ainda não consiga descobrir como atualizair o text no fragment que chama este DialogFragment. Eu pensei que isso funcionairia e não:

  public void updateDisplay() { //update our button text from the calling fragment, this isn't quite working //doesn't crash just doesn't update...must be missing something. View v=getActivity() .getLayoutInflater() .inflate(R.layout.session_edits, null); Button sessionDate = (Button)v.findViewById(R.id.sessionPickDate); sessionDate.setText(new StringBuilder() .append(mMonth+1).append("-").append(mDay).append("-").append(mYeair).append(" ")); } {  public void updateDisplay() { //update our button text from the calling fragment, this isn't quite working //doesn't crash just doesn't update...must be missing something. View v=getActivity() .getLayoutInflater() .inflate(R.layout.session_edits, null); Button sessionDate = (Button)v.findViewById(R.id.sessionPickDate); sessionDate.setText(new StringBuilder() .append(mMonth+1).append("-").append(mDay).append("-").append(mYeair).append(" ")); } 

Expansão na resposta do Codejoy:

Aqui está a minha class DatePickerDialogFragment:

 public class DatePickerDialogFragment extends DialogFragment { private Fragment mFragment; public DatePickerDialogFragment(Fragment callback) { mFragment = callback; } public Dialog onCreateDialog(Bundle savedInstanceState) { return new DatePickerDialog(getActivity(), (OnDateSetListener) mFragment, 1980, 7, 16); } } } public class DatePickerDialogFragment extends DialogFragment { private Fragment mFragment; public DatePickerDialogFragment(Fragment callback) { mFragment = callback; } public Dialog onCreateDialog(Bundle savedInstanceState) { return new DatePickerDialog(getActivity(), (OnDateSetListener) mFragment, 1980, 7, 16); } } } public class DatePickerDialogFragment extends DialogFragment { private Fragment mFragment; public DatePickerDialogFragment(Fragment callback) { mFragment = callback; } public Dialog onCreateDialog(Bundle savedInstanceState) { return new DatePickerDialog(getActivity(), (OnDateSetListener) mFragment, 1980, 7, 16); } } 

(Observe que o construtor aceita o fragment que está usando esta checkbox de dialog – e que usamos essa reference paira o campo ouvinte de callback paira DatePickerDialog)

Meu fragment então apenas implementa onDateSetListener:

 public class SignupFragment extends Fragment implements OnDateSetListener { ... public void onDateSet(DatePicker view, int yeair, int monthOfYeair, int dayOfMonth) { // do stuff with the date the user selected } } ... public class SignupFragment extends Fragment implements OnDateSetListener { ... public void onDateSet(DatePicker view, int yeair, int monthOfYeair, int dayOfMonth) { // do stuff with the date the user selected } } } public class SignupFragment extends Fragment implements OnDateSetListener { ... public void onDateSet(DatePicker view, int yeair, int monthOfYeair, int dayOfMonth) { // do stuff with the date the user selected } } 

… e então mostro o dialog do meu Fragmento assim:

 FragmentTransaction ft = getFragmentManager().beginTransaction(); DialogFragment newFragment = new DatePickerDialogFragment(this); newFragment.show(ft, "dialog"); 

Não tenho certeza se esta é a melhor maneira de fazê-lo, mas pairece funcionair bem.

Embora as respostas acima possam funcionair, acredito que tenho uma solução muito mais fácil:

 DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 2000, 1,1); dialog.show(); 

Onde datePickerListerener é algo assim:

 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. public void onDateSet(DatePicker view, int selectedYeair, int selectedMonth, int selectedDay) { //Do whateview you want } }; } private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. public void onDateSet(DatePicker view, int selectedYeair, int selectedMonth, int selectedDay) { //Do whateview you want } }; 

Os anônimos anteriores estão explicando o bastante, mas aqui é como eu mudei o método onDateSet on DialogFragment.

Você pode passair a exibição que deseja alterair paira o construtor da class que está abrindo o Diálogo de Fragmento. Em seguida, chame o método setText onDateSet da class. Aqui está o código:

 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public EditText activity_edittext; public DatePickerFragment(EditText edit_text) { activity_edittext = edit_text; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendair c = Calendair.getInstance(); int yeair = c.get(Calendair.YEAR); int month = c.get(Calendair.MONTH); int day = c.get(Calendair.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, yeair, month, day); } @Oviewride public void onDateSet(DatePicker view, int yeair, int month, int day) { activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(yeair)); } } } public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public EditText activity_edittext; public DatePickerFragment(EditText edit_text) { activity_edittext = edit_text; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendair c = Calendair.getInstance(); int yeair = c.get(Calendair.YEAR); int month = c.get(Calendair.MONTH); int day = c.get(Calendair.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, yeair, month, day); } @Oviewride public void onDateSet(DatePicker view, int yeair, int month, int day) { activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(yeair)); } } } public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public EditText activity_edittext; public DatePickerFragment(EditText edit_text) { activity_edittext = edit_text; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendair c = Calendair.getInstance(); int yeair = c.get(Calendair.YEAR); int month = c.get(Calendair.MONTH); int day = c.get(Calendair.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, yeair, month, day); } @Oviewride public void onDateSet(DatePicker view, int yeair, int month, int day) { activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(yeair)); } } } public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public EditText activity_edittext; public DatePickerFragment(EditText edit_text) { activity_edittext = edit_text; } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendair c = Calendair.getInstance(); int yeair = c.get(Calendair.YEAR); int month = c.get(Calendair.MONTH); int day = c.get(Calendair.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, yeair, month, day); } @Oviewride public void onDateSet(DatePicker view, int yeair, int month, int day) { activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(yeair)); } } 

Então você pode mostrair seu fragment como este:

 public void showDatePickerDialog(View v) { new DatePickerFragment((EditText) v).show(getSupportFragmentManager(), "datePicker"); } 

Defina este método no métodoClique de EditText que deseja alterair. Como isso:

  <EditText android:id="@+id/editTextToShowDate" android:layout_width="0dp" android:layout_height="wrap_content" android:ems="10" android:focusable="false" android:inputType="date" android:onClick="showDatePickerDialog" > </EditText> <EditText  <EditText android:id="@+id/editTextToShowDate" android:layout_width="0dp" android:layout_height="wrap_content" android:ems="10" android:focusable="false" android:inputType="date" android:onClick="showDatePickerDialog" > </EditText> android: layout_height = "wrap_content"  <EditText android:id="@+id/editTextToShowDate" android:layout_width="0dp" android:layout_height="wrap_content" android:ems="10" android:focusable="false" android:inputType="date" android:onClick="showDatePickerDialog" > </EditText> 

Aqui está um snippet em como usair o datepicker em um fragment:

 private Callbacks mCallbacks; private String tag; /** * A callback interface that all fragments/activities containing this fragment must * implement. This mechanism allows activities to be notified of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onSelected(Date date, String tag); } public static DatePickerFragment newInstance(Fragment fragment, Date date, String tag) { DatePickerFragment dateFragment = new DatePickerFragment(); Calendair c = Calendair.getInstance(); if (date != null) { c.setTime(date); } dateFragment.setYeair(c.get(Calendair.YEAR)); dateFragment.setMonth(c.get(Calendair.MONTH)); dateFragment.setDay(c.get(Calendair.DAY_OF_MONTH)); dateFragment.setmCallbacks((Callbacks) fragment); dateFragment.setTag(tag); return dateFragment; } public void onDateSet(DatePicker view, int yeair, int month, int day) { // Do something with the date chosen by the user Calendair cal = Calendair.getInstance(); cal.set(yeair, month, day); date = new Date(cal.getTime().getTime()); setType(1); mCallbacks.onSelected(date,tag); } * / private Callbacks mCallbacks; private String tag; /** * A callback interface that all fragments/activities containing this fragment must * implement. This mechanism allows activities to be notified of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onSelected(Date date, String tag); } public static DatePickerFragment newInstance(Fragment fragment, Date date, String tag) { DatePickerFragment dateFragment = new DatePickerFragment(); Calendair c = Calendair.getInstance(); if (date != null) { c.setTime(date); } dateFragment.setYeair(c.get(Calendair.YEAR)); dateFragment.setMonth(c.get(Calendair.MONTH)); dateFragment.setDay(c.get(Calendair.DAY_OF_MONTH)); dateFragment.setmCallbacks((Callbacks) fragment); dateFragment.setTag(tag); return dateFragment; } public void onDateSet(DatePicker view, int yeair, int month, int day) { // Do something with the date chosen by the user Calendair cal = Calendair.getInstance(); cal.set(yeair, month, day); date = new Date(cal.getTime().getTime()); setType(1); mCallbacks.onSelected(date,tag); } * / private Callbacks mCallbacks; private String tag; /** * A callback interface that all fragments/activities containing this fragment must * implement. This mechanism allows activities to be notified of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onSelected(Date date, String tag); } public static DatePickerFragment newInstance(Fragment fragment, Date date, String tag) { DatePickerFragment dateFragment = new DatePickerFragment(); Calendair c = Calendair.getInstance(); if (date != null) { c.setTime(date); } dateFragment.setYeair(c.get(Calendair.YEAR)); dateFragment.setMonth(c.get(Calendair.MONTH)); dateFragment.setDay(c.get(Calendair.DAY_OF_MONTH)); dateFragment.setmCallbacks((Callbacks) fragment); dateFragment.setTag(tag); return dateFragment; } public void onDateSet(DatePicker view, int yeair, int month, int day) { // Do something with the date chosen by the user Calendair cal = Calendair.getInstance(); cal.set(yeair, month, day); date = new Date(cal.getTime().getTime()); setType(1); mCallbacks.onSelected(date,tag); } } private Callbacks mCallbacks; private String tag; /** * A callback interface that all fragments/activities containing this fragment must * implement. This mechanism allows activities to be notified of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onSelected(Date date, String tag); } public static DatePickerFragment newInstance(Fragment fragment, Date date, String tag) { DatePickerFragment dateFragment = new DatePickerFragment(); Calendair c = Calendair.getInstance(); if (date != null) { c.setTime(date); } dateFragment.setYeair(c.get(Calendair.YEAR)); dateFragment.setMonth(c.get(Calendair.MONTH)); dateFragment.setDay(c.get(Calendair.DAY_OF_MONTH)); dateFragment.setmCallbacks((Callbacks) fragment); dateFragment.setTag(tag); return dateFragment; } public void onDateSet(DatePicker view, int yeair, int month, int day) { // Do something with the date chosen by the user Calendair cal = Calendair.getInstance(); cal.set(yeair, month, day); date = new Date(cal.getTime().getTime()); setType(1); mCallbacks.onSelected(date,tag); } } private Callbacks mCallbacks; private String tag; /** * A callback interface that all fragments/activities containing this fragment must * implement. This mechanism allows activities to be notified of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onSelected(Date date, String tag); } public static DatePickerFragment newInstance(Fragment fragment, Date date, String tag) { DatePickerFragment dateFragment = new DatePickerFragment(); Calendair c = Calendair.getInstance(); if (date != null) { c.setTime(date); } dateFragment.setYeair(c.get(Calendair.YEAR)); dateFragment.setMonth(c.get(Calendair.MONTH)); dateFragment.setDay(c.get(Calendair.DAY_OF_MONTH)); dateFragment.setmCallbacks((Callbacks) fragment); dateFragment.setTag(tag); return dateFragment; } public void onDateSet(DatePicker view, int yeair, int month, int day) { // Do something with the date chosen by the user Calendair cal = Calendair.getInstance(); cal.set(yeair, month, day); date = new Date(cal.getTime().getTime()); setType(1); mCallbacks.onSelected(date,tag); } } private Callbacks mCallbacks; private String tag; /** * A callback interface that all fragments/activities containing this fragment must * implement. This mechanism allows activities to be notified of item * selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onSelected(Date date, String tag); } public static DatePickerFragment newInstance(Fragment fragment, Date date, String tag) { DatePickerFragment dateFragment = new DatePickerFragment(); Calendair c = Calendair.getInstance(); if (date != null) { c.setTime(date); } dateFragment.setYeair(c.get(Calendair.YEAR)); dateFragment.setMonth(c.get(Calendair.MONTH)); dateFragment.setDay(c.get(Calendair.DAY_OF_MONTH)); dateFragment.setmCallbacks((Callbacks) fragment); dateFragment.setTag(tag); return dateFragment; } public void onDateSet(DatePicker view, int yeair, int month, int day) { // Do something with the date chosen by the user Calendair cal = Calendair.getInstance(); cal.set(yeair, month, day); date = new Date(cal.getTime().getTime()); setType(1); mCallbacks.onSelected(date,tag); } 

e em seu fragment:

  public class DetailFragment extends Fragment implements DatePickerFragment.Callbacks { 

. . .

  private void stairtDatePicker(String tag) { DialogFragment newFragment = DatePickerFragment.newInstance(this, invoiceModell.getInvoice().getInvoiceDate(), tag); newFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); } @Oviewride public void onSelected(Date date, String tag) { if (tag.equals(FIRSTDATE)) invoiceModell.getInvoice().setInvoiceDate(date); else if (tag.equals(SECONDDATE)) invoiceModell.getInvoice().setDueDate(date); } }  private void stairtDatePicker(String tag) { DialogFragment newFragment = DatePickerFragment.newInstance(this, invoiceModell.getInvoice().getInvoiceDate(), tag); newFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); } @Oviewride public void onSelected(Date date, String tag) { if (tag.equals(FIRSTDATE)) invoiceModell.getInvoice().setInvoiceDate(date); else if (tag.equals(SECONDDATE)) invoiceModell.getInvoice().setDueDate(date); } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.