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


当前回答

日期选择对话框

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

                DatePickerDialog datePickerDialog =new DatePickerDialog(Expenture_Activity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        et_from.setText(dayOfMonth+"/"+month+"/"+year);
                    }
                },year,month,day);
                datePickerDialog.show();

其他回答

    EditText text = (EditText) findViewById(R.id.editText);

    text.setOnClickListener(new OnClickListener(){
       @RequiresApi(api = Build.VERSION_CODES.N)
       @Override
       public void onClick(View view) {

             DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
               @Override
               public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                   String date = i2 + "/" + (++i1) + "/" + i;
                   text.setText(date);
                }
        },year,month,date);
       }
    });  

    datePickerDialog.show();

您还可以通过这些语句设置最小日期和最大日期。

   datepickerDialoge.getDatepicker().setMinDate(long Date);

   datepickerDialoge.getDatepicker().setMaxDate(long Date);

注意:在datepickerDialog.show()之前添加这些行;声明 你会得到这样的日期=12/2/2017。

我希望我的回答能有所帮助。

 selectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatePickerDialog  mdiDialog =new DatePickerDialog(mContext,new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Toast.makeText(getApplicationContext(),year+ " "+monthOfYear+" "+dayOfMonth,Toast.LENGTH_LONG).show();


                    }
                }, year, month, date);
                mdiDialog.show();

            }

        });

我认为最好重写EditText类…

public class EditTextDP extends android.support.v7.widget.AppCompatEditText implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
    public EditTextDP(Context context) {
        super(context);
        setOnClickListener(this);
        setFocusable(false);
    }

    public EditTextDP(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
        setFocusable(false);
    }

    public EditTextDP(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnClickListener(this);
        setFocusable(false);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        setText(new StringBuilder().append(dayOfMonth).append("/").append(monthOfYear + 1).append("/").append(year));
    }

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        DatePickerDialog dialog = new DatePickerDialog(getContext(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
}

然后在XML中使用它…

            <*.*.*.EditTextDP
                android:id="@+id/c1_dob_hm"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round_edittext_white"
                android:ems="10"
                android:padding="8dp">

            </*.*.*.EditTextDP>

试试这个

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();
                }
            });
        }

    });

这在2020年1月对我有效

XML部分:

  <EditText
            android:id="@+id/etDOB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:editable="false"
            android:ems="10"
            android:hint="Enter Your Date of Birth" />

Java的部分:

         final Calendar myCalendar = Calendar.getInstance();

         userDOBView  = (EditText)findViewById(R.id.etDOB);


    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){
      @Override
      public void onDateSet(DatePicker view, int year, int monthOfYear, int  dayOfMonth) {
             myCalendar.set(Calendar.YEAR, year);
             myCalendar.set(Calendar.MONTH, monthOfYear);
             myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
             updateLabel();
      }
    };

    userDOBView.setOnTouchListener(new View.OnTouchListener(){
      @Override
      public boolean onTouch(View v, MotionEvent event){
       if(event.getAction() == MotionEvent.ACTION_DOWN){
         new DatePickerDialog("YourClassName".this, date, 
             myCalendar.get(Calendar.YEAR), 
             myCalendar.get(Calendar.MONTH),         
             myCalendar.get(Calendar.DAY_OF_MONTH)).show();
          }
          return true;
       }
    });
private void updateLabel() {
  String myFormat = "MM/dd/yyyy"; //In which you need put here
  SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

  // edittext.setText(sdf.format(myCalendar.getTime()));
  userDOBView.setText(sdf.format(myCalendar.getTime()));
}