【问题标题】:How to show Cancel button on DatePicker?如何在 DatePicker 上显示取消按钮?
【发布时间】:2015-06-10 04:38:37
【问题描述】:

我正在使用显示DatePickerDialogFragment。如何在对话框中显示“取消”按钮?

【问题讨论】:

标签: android android-dialogfragment android-datepicker


【解决方案1】:

试试我从THIS找到的这段代码


DatePickerDialog dialog = new DatePickerDialog(this,
              mDateSetListener,
              year, month, day);

  dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
       if (which == DialogInterface.BUTTON_NEGATIVE) {
          // Do Stuff
       }
    }
  });

【讨论】:

    【解决方案2】:

    使用此代码并根据您的方便修改 UI。

    /**
         * Prepares & shows the Dialog for selecting date.
         */
        private void prepareDateDialog()
        {
            final Dialog dialog = new Dialog(this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
            dialog.setContentView(R.layout.dialog_date_picker);
            datePicker = (DatePicker) dialog.findViewById(R.id.dialog_date_picker_date);
    
            // initialize DatePicker with the previously initialized values.
            datePicker.init(year, month - 1, dayOfMonth, null);
            TextView tvDone = (TextView) dialog.findViewById(R.id.dialog_date_picker_tv_done);
            TextView tvCancel = (TextView) dialog.findViewById(R.id.dialog_date_picker_tv_cancel);
            tvCancel.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    dialog.dismiss();
                }
            });
    
            tvDone.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    dialog.dismiss();
                    setDate();
                }
            });
            dialog.show();
        }
    

    setDate 方法..

    /**
         * Sets the date selected by the user.
         */
        private void setDate()
        {
            dayOfMonth = datePicker.getDayOfMonth();
            month = (datePicker.getMonth()) + 1;
            year = datePicker.getYear();
    
            Calendar c = Calendar.getInstance();
            c.set(Calendar.DATE, dayOfMonth);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.YEAR, year);
    
            String monthCount = "" + month;
            String day = dayOfMonth + "";
            if (dayOfMonth < 9)
                day = "0" + dayOfMonth;
            if (month < 9)
                monthCount = "0" + month;
    
            selectedDate = day + "-" + monthCount + "-" + year;
            tvDate.setText(selectedDate);
            tvBirthday.setText(selectedDate);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多