Launching a DatePicker from edit text

后端 未结 1 839
小蘑菇
小蘑菇 2021-01-28 21:48

I am using Date picker on one of the edittext

ID of edit text is :: android:id=\"@+id/DATE_EDIT_TEXT_ID\"

相关标签:
1条回答
  • 2021-01-28 22:01
    // try this
    public class SearchPage extends FragmentActivity {
    
        EditText mEdit;
        DialogFragment newFragment;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.search_page);
            mEdit = (EditText)findViewById(R.id.DATE_EDIT_TEXT_ID);
            newFragment = new SelectDateFragment();
            mEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   newFragment.show(getFragmentManager(), "DatePicker");
                }
            });
        }
    
        public void populateSetDate(int year, int month, int day) {
            mEdit.setText(day+"/"+month+"/"+year);
        }
        public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final Calendar calendar = Calendar.getInstance();
                int yy = calendar.get(Calendar.YEAR);
                int mm = calendar.get(Calendar.MONTH);
                int dd = calendar.get(Calendar.DAY_OF_MONTH);
                return new DatePickerDialog(getActivity(), this, yy, mm, dd);
            }
    
            public void onDateSet(DatePicker view, int yy, int mm, int dd) {
                populateSetDate(yy, dd, mm+1);
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题