How to get OnClick in DatePickerDialog.OnDateSetListener?

后端 未结 2 841
旧时难觅i
旧时难觅i 2021-01-07 04:50

I am using the following method to pop up the dialog to pick the date.

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDi         


        
2条回答
  •  天涯浪人
    2021-01-07 05:14

    Date Picker Dialog you are using is deprecated so I recommend you to don't use that.

    You can implement the Date picker in two ways(that I know of)

    1. Using DialogFragment
    2. Using AlertDialog

    Using DialogFragment:

    public class MainActivity extends FragmentActivity {
        EditText text;
        Button b;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            b=(Button)findViewById(R.id.button1);
            text=(EditText)findViewById(R.id.editText1);
            b.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    DateDialogFragment datepicker=new DateDialogFragment();
                    datepicker.show(getSupportFragmentManager(), "showDate");
                }
            });
        }
    
        public class DateDialogFragment extends DialogFragment  implements DatePickerDialog.OnDateSetListener{
    
            public DateDialogFragment()
            {
            }
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                Calendar cal=Calendar.getInstance();
                int year=cal.get(Calendar.YEAR);
                int month=cal.get(Calendar.MONTH);
                int day=cal.get(Calendar.DAY_OF_MONTH);
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                showSetDate(year,monthOfYear,dayOfMonth);
            }
    
            }
    
        public void showSetDate(int year,int month,int day) {
        text.setText(year+"/+"+month+"/"+day);
        }
    }
    

    Check this sample and implement the same in your Activity.

    Using Alert Dialog:

    Using the second one is pretty simple

    Create layout in your res/layout folder and place DatePicker in the layout

     LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = (View) inflater.inflate(R.layout.yourlayout, null);
    DatePicker picker=(DatePicker)view.findViewById(R.id.datepicker);
    
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     builder.setView(view).
            builder.setMessage(R.string.dialog_fire_missiles)
                   .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // FIRE ZE MISSILES!
                       }
                   })
                   .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // User cancelled the dialog
                       }
                   });
            // Create the AlertDialog object and return it
            return builder.create();
    

提交回复
热议问题