1

我是安卓新手。我的布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="26-01-2010" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calender" />

</LinearLayout>

我有一个编辑框和按钮。当我点击按钮时,我想打开一个日历并能够从日历中选择一个日期。此外,必须将所选日期设置为编辑框。

4

3 回答 3

4

在文本视图或按钮或编辑文本单击上调用方法 SelectDate

 private void SelectDate() {

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);


    showDialog(DATE_PICKER_ID);

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_PICKER_ID:

            // open datepicker dialog.
            // set date picker for current date
            // add pickerListener listner to date picker
            return new DatePickerDialog(this, pickerListener, year, month,day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    @Override
    public void onDateSet(DatePicker view, int selectedYear,
                          int selectedMonth, int selectedDay) {

        year  = selectedYear;
        month = selectedMonth;
        day   = selectedDay;

        // Show selected date
        birthdate_edt.setText(new StringBuilder().append(month + 1)
                .append("-").append(day).append("-").append(year)
                .append(" "));

    }
};
于 2017-04-03T19:41:55.373 回答
1

尝试这样的事情:

button.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v) {
      Dialog dlg = new DatePickerDialog(YourActivity.this, new DatePickerDialog.OnDateSetListener() {
         public void onDateSet(DatePicker view, int year, int month, int day) {
            yourEditText.setText(String.format("%02d.%02d.%04d", day, month+1, year);
         }
      }
   }
});

您还可以将您的edittext设置为不可聚焦以防止手动输入值

于 2012-11-23T10:26:00.623 回答
0

我这样做了,它对我有用!

弹出 DatePickerDialog 的方法

public void openDatePickerDialog(final View v) {

    Calendar cal = Calendar.getInstance();
    // Get Current Date
    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            (view, year, monthOfYear, dayOfMonth) -> {
                String selectedDate = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
                switch (v.getId()) {
                    case R.id.editTextDob:
                        ((EditText)v).setText(selectedDate);
                        dob = selectedDate;
                        Log.e("DOB selected",dob);
                        break;
                }
            }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));


    datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
    datePickerDialog.show();
}

然后简单调用上面的方法 onClick 监听器内幕 onCreateActivity

edtDOB.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus){
                openDatePickerDialog(view);
            }
        }
    });
edtDOB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openDatePickerDialog(view);
        }
    });
于 2022-02-05T11:25:38.870 回答