从日历中选择日期并在TextView中显示日期的最佳方法

时间:2014-01-28 14:18:32

标签: android android-fragments android-calendar

我搜索了很多,需要知道从日历中选择日期的最佳方式,我已经在尝试片段,例如

FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction();

DialogFragment fragment = new DatePickerDialogFragment(MainActivity.this); fragment.show(transaction,“date_dialog”);

但是有关于进口的错误,因为它们相互矛盾我相信...... 真的需要一个出路,有没有人有一个很好的解决方案,请让我知道我需要它!

3 个答案:

答案 0 :(得分:2)

我喜欢用Dialog做到这一点。 首先,在xml中的android:focusable="false"上设置EditText

然后你可以尝试这样的事情:

yourEditText = (EditText) findViewById(R.id.yourEditTextID);
        yourEditText.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // To show current date in the datepicker
                Calendar mcurrentDate = Calendar.getInstance();
                mYear = mcurrentDate.get(Calendar.YEAR);
                mMonth = mcurrentDate.get(Calendar.MONTH);
                mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker = new DatePickerDialog(yourActivity.this, new OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        Calendar myCalendar = Calendar.getInstance();
                        myCalendar.set(Calendar.YEAR, selectedyear);
                        myCalendar.set(Calendar.MONTH, selectedmonth);
                        myCalendar.set(Calendar.DAY_OF_MONTH, selectedday);
                        String myFormat = "dd/MM/yy"; //Change as you need
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.FRANCE);
                        yourEditText.setText(sdf.format(myCalendar.getTime()));

                        mDay = selectedday;
                        mMonth = selectedmonth;
                        mYear = selectedyear;
                    }
                }, mYear, mMonth, mDay);
                //mDatePicker.setTitle("Select date");
                mDatePicker.show();
            }
        });

mYear,mMonth和mDay是全球性的。

答案 1 :(得分:0)

您可以尝试这样的事情

DatePicker mDlgDatePicker = new DatePickerDialog(getActivity(), new OnDateSetListener() {
@Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    mEdtxDate.setText(year + "-" + (monthOfYear + 1 < 10 ? "0" : "") + (monthOfYear + 1) + "-"
                            + (dayOfMonth < 10 ? "0" : "") + dayOfMonth);
                }
  }, 2014, 1, 1);
  mDlgDatePicker.show();

答案 2 :(得分:0)

选择日期为默认日期的 KOTLIN 日期选择器

  1. 最初将此代码添加为全局范围,位于 onCreate() 之上或之外的某处

     //this will show default date as current date initially
     private val currentDate: Calendar = Calendar.getInstance()
     private var year = currentDate[Calendar.YEAR]
     private var month = currentDate[Calendar.MONTH]
     private var day = currentDate[Calendar.DAY_OF_MONTH]
    
  2. 在您的 TextView 上,添加此 OnClickListener:

     binding.tvDate.setOnClickListener {
         selectDate()
     }
    
  3. 这是您的 selectDate() 函数:

     private fun selectDate() {
     val mDatePicker = DatePickerDialog(
         this,
         { _, selectedYear, selectedMonth, selectedDay ->
             val myFormat = AppConstants.DateFormat.DF_dd_MM_yy
             val sdf = SimpleDateFormat(myFormat, Locale.getDefault())
             day = selectedDay
             month = selectedMonth
             year = selectedYear
             currentDate.set(year, month, day)
             binding.tvDate.text = sdf.format(currentDate.time)
         }, year, month, day
     )
    
     mDatePicker.show()
     mDatePicker.getButton(AlertDialog.BUTTON_POSITIVE)
         .setTextColor(ContextCompat.getColor(this, R.color.purple))
    
     mDatePicker.getButton(AlertDialog.BUTTON_NEGATIVE)
         .setTextColor(ContextCompat.getColor(this, R.color.purple))
    

    }

这里。 DF_dd_MM_yyyyconst val DF_dd_MM_yyyy = "dd/MM/yyyy"