我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。


当前回答

如果你需要一个单一的功能,所有的活动,当调用将打开日期选择器,然后返回我选择的日期,然后为所有创建一个接口

public interface OnDatePickerLitionarCallBack {

    void DatePicker(String selectedDate);
}

然后在活动中调用接口

 public class MapsActivity extends FragmentActivity implements OnDatePickerLitionarCallBack {
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        findViewById(R.id.date_pick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                date_Picker(MapsActivity.this,MapsActivity.this);
            }
        });
    }
     @Override
        public void DatePicker(String selectedDate) {
        
    
        }
    }

实现接口后,创建一个公共类用于访问任何活动或片段

public class Commons{

    public static void date_Picker(Context context, OnDatePickerLitionarCallBack onDatePickerLitionarCallBack) {
        final java.util.Calendar myCalendar = java.util.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(java.util.Calendar.YEAR, year);
                myCalendar.set(java.util.Calendar.MONTH, monthOfYear);
                myCalendar.set(java.util.Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd,MMM yyyy"; //In which you need put here
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(myFormat, Locale.US);
   
                onDatePickerLitionarCallBack.DatePicker(sdf.format(myCalendar.getTime()));

            }
        };
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, date, myCalendar
                .get(java.util.Calendar.YEAR), myCalendar.get(java.util.Calendar.MONTH),
                myCalendar.get(java.util.Calendar.DAY_OF_MONTH));


        datePickerDialog.show();

    }
}

其他回答

如果你需要一个单一的功能,所有的活动,当调用将打开日期选择器,然后返回我选择的日期,然后为所有创建一个接口

public interface OnDatePickerLitionarCallBack {

    void DatePicker(String selectedDate);
}

然后在活动中调用接口

 public class MapsActivity extends FragmentActivity implements OnDatePickerLitionarCallBack {
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        findViewById(R.id.date_pick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                date_Picker(MapsActivity.this,MapsActivity.this);
            }
        });
    }
     @Override
        public void DatePicker(String selectedDate) {
        
    
        }
    }

实现接口后,创建一个公共类用于访问任何活动或片段

public class Commons{

    public static void date_Picker(Context context, OnDatePickerLitionarCallBack onDatePickerLitionarCallBack) {
        final java.util.Calendar myCalendar = java.util.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(java.util.Calendar.YEAR, year);
                myCalendar.set(java.util.Calendar.MONTH, monthOfYear);
                myCalendar.set(java.util.Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd,MMM yyyy"; //In which you need put here
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(myFormat, Locale.US);
   
                onDatePickerLitionarCallBack.DatePicker(sdf.format(myCalendar.getTime()));

            }
        };
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, date, myCalendar
                .get(java.util.Calendar.YEAR), myCalendar.get(java.util.Calendar.MONTH),
                myCalendar.get(java.util.Calendar.DAY_OF_MONTH));


        datePickerDialog.show();

    }
}

这是我怎么做的:类似于选择的答案,但使用一个静态对话框类,所以它更可重用,也使用onFocusChange而不是onClick。

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                DialogFragment datePickerFragment = new DatePickerFragment() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Log.d(TAG, "onDateSet");
                        Calendar c = Calendar.getInstance();
                        c.set(year, month, day);
                        editText.setText(df.format(c.getTime()));
                        nextField.requestFocus(); //moves the focus to something else after dialog is closed
                    }
                };
                datePickerFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
            }
        }
    });

datepicker对话框。

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        //blah
    }
}

使用@DrunkenDaddy扩展函数,至少在我的情况下(以编程方式创建EditText),它在第一次单击时显示键盘,然后在第二次单击时显示日期选择器。

显然,将isFocusable和isFocusableInTouchMode设置为false是无关紧要的,因为第一次点击总是被EditText解释为焦点变化。因此,当EditText获得焦点时,我必须触发单击。现在它像预期的那样工作:第一次点击它直接打开日期选择器,而不是键盘(感谢设置showSoftInputOnFocus为false):

fun EditText.transformIntoDatePicker(context: Context, format: String = "dd/MM/yyyy", maxDate: Date? = null) {
    isClickable = true
    showSoftInputOnFocus = false
    isCursorVisible = false

    setOnFocusChangeListener { _, hasFocus ->  if (hasFocus) callOnClick()}

    val myCalendar = Calendar.getInstance()
    val datePickerOnDataSetListener =
        DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth ->
            myCalendar.set(Calendar.YEAR, year)
            myCalendar.set(Calendar.MONTH, monthOfYear)
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            val sdf = SimpleDateFormat(format, Locale.getDefault())
            setText(sdf.format(myCalendar.time))
    }

    setOnClickListener {
        DatePickerDialog(
             context, datePickerOnDataSetListener, 
             myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
             myCalendar.get(Calendar.DAY_OF_MONTH)
        ).run {
            maxDate?.time?.also { datePicker.maxDate = it }
            show()
    }
}
public class DatePickerActivity extends AppCompatActivity {
    Button button;
    static TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.btn_click);
        textView= (TextView) findViewById(R.id.txt_date);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment newFragment=new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datepicker");
            }
        });

    }

    public class DatePickerFragment extends DialogFragment implements    DatePickerDialog.OnDateSetListener{
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int  day) {
            String years=""+year;
            String months=""+(monthOfYear+1);
            String days=""+day;
            if(monthOfYear>=0 && monthOfYear<9){
                months="0"+(monthOfYear+1);
            }
            if(day>0 && day<10){
                days="0"+day;

            }
            textView.setText(days+"/"+months+"/"+years);


        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            //use the current date as the default date in the picker
            final Calendar c=Calendar.getInstance();
            int year=c.get(Calendar.YEAR);
            int month=c.get(Calendar.MONTH);
            int day=c.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog=null;
            datePickerDialog=new DatePickerDialog(getActivity(), this, year,  month, day);

            return datePickerDialog;
        }

    }

}

试试这个

btn=(Button)findViewById(R.id.chs);
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.setCanceledOnTouchOutside(true);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.setContentView(R.layout.datepickerdialog);
            dp=(DatePicker) dialog.findViewById(R.id.btp);
            dialog.show();
            ok=(Button)dialog.findViewById(R.id.btn1);
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }

    });