Android: DatePicker and DatePicker Dialog

前端 未结 8 550
梦如初夏
梦如初夏 2020-12-24 09:23

I have this code here on the options menu

Dialog dialog = new Dialog(ScheduleActivity.this);
dialog.setTitle(\"Add Event\");
dialog.setContentView(R.layout.         


        
相关标签:
8条回答
  • 2020-12-24 09:51

    This is my helper function which get context and TextView as parameter and sets Date on that text view when user select a date:

            public static void showDate(final Context context, final TextView textView) {
    
        if (textView != null) {
            final Calendar myCalendar = Calendar.getInstance();
            DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    // TODO Auto-generated method stub
                    myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH, monthOfYear);
                    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    String myFormat = "MM/dd/yy"; // In which you need put here
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                    //UIHelper.showLongToastInCenter(context, sdf.format(myCalendar.getTime()));
                    textView.setText(sdf.format(myCalendar.getTime()));
                }
    
            };
            new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        } else {
            UIHelper.showLongToastInCenter(context, "Unable to show Date picker");
        }
    }
    
    0 讨论(0)
  • 2020-12-24 09:56

    Try this code it is definately work:

                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
    
                DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                        new DatePickerDialog.OnDateSetListener() {
    
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
    
                                birth_Date.setText(dayOfMonth + "-"
                                        + (monthOfYear + 1) + "-" + year);
                            }
                        }, mYear, mMonth, mDay);
                dpd.show();
    
    0 讨论(0)
  • 2020-12-24 09:58

    Here I Provide Some Code Hope It Helps You..

    public class NewReminder extends Activity {
    private static final int DATE_DIALOG_ID = 1;
    private int year;
    private int month;
    private int day;
    EditText editTextDate;
    private String currentDate;
    private Context context;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnewreminder);
        initialize();
        context = getApplicationContext();
        OnClickListener listenerDate = new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
                final Calendar c = Calendar.getInstance();
                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH);
                showDialog(DATE_DIALOG_ID);
            }
        };
        editTextDate.setOnClickListener(listenerDate);
    
    }
    
    private void initialize() {
        // TODO Auto-generated method stub
    
        editTextDate = (EditText) findViewById(R.id.editTextDate);
    
    }
    
    
    private void updateDisplay() {
        currentDate = new StringBuilder().append(day).append(".")
                .append(month + 1).append(".").append(year).toString();
    
        Log.i("DATE", currentDate);
    }
    
    OnDateSetListener myDateSetListener = new OnDateSetListener() {
    
        @Override
        public void onDateSet(DatePicker datePicker, int i, int j, int k) {
    
            year = i;
            month = j;
            day = k;
            updateDisplay();
            editTextDate.setText(currentDate);
        }
    };
    
    
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, myDateSetListener, year, month,
                    day);
        }
        return null;
    }
    }
    
    0 讨论(0)
  • 2020-12-24 10:05

    In XML add a TextView and a Button

    <TextView
        android:id="@+id/searchText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
    <Button
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search" />
    

    Add following code in java file

    public class DatePickerDialogExample extends Activity {
    
        TextView txtDate;
        private int mYear, mMonth, mDay, mHour, mMinute;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
            txtDate = (TextView) findViewById(R.id.searchText);
            Button search = (Button) findViewById(R.id.search);
            search.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // Process to get Current Date
                final Calendar c = Calendar.getInstance();
                mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);
    
                // Launch Date Picker Dialog
                DatePickerDialog dpd = new DatePickerDialog(PrayTimeActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
    
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                                // Display Selected date in textbox
                                txtDate.setText(dayOfMonth + "-"
                                        + (monthOfYear + 1) + "-" + year);
    
                            }
                        }, mYear, mMonth, mDay);
                    dpd.show();
                }
            });
    
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 10:06

    You can try this code.. it will surely helpful to u.. No Doubt!

        protected Dialog onCreateDialog(int id) {
    
        Calendar c=Calendar.getInstance();
        int Sysday=c.get(Calendar.DAY_OF_MONTH);
        int Sysmonth=c.get(Calendar.MONTH);
        int Sysyear=c.get(Calendar.YEAR);
    
        int Sysmin=c.get(Calendar.MINUTE);
        int Syshour=c.get(Calendar.HOUR);
    
    
        switch (id) {
        case TIME_DIALOG:
    
            return new TimePickerDialog(this, myTimePicker , Syshour, Sysmin, false);
    
        case DATE_DIALOG:
            return new DatePickerDialog(this, myDatePicker, Sysyear, Sysmonth, Sysday);
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-12-24 10:09
    Calendar c1 = Calendar.getInstance();    
    int year = c1.get(Calendar.YEAR);
    int month = c1.get(Calendar.MONTH);
    int day = c1.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog.OnDateSetListener myDateListener  = new DatePickerDialog.OnDateSetListener() {
    
           public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
                       if(arg0.isShown()){
    
                                         //do further code here
    
                                  }
                                }
    
                              };
            DatePickerDialog dp = new DatePickerDialog(YourActivity.this, myDateListener, year, month, day);
            dp.show();      
    
    0 讨论(0)
提交回复
热议问题