在点击的EditText的打开DatePickerDialog需要两次点击 [英] Open a DatePickerDialog on Click of EditText takes two clicks

查看:279
本文介绍了在点击的EditText的打开DatePickerDialog需要两次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开上编辑文本的点击日历。从那以后,我要设置该用户从编辑文本的日历选择日期。问题是,只有当我点击的EditText第二次则日历打开。请帮我解决这个问题(为什么日历不开首次)。

I want to open a Calendar on the click of Edit text. After that I want to set the date which the user selects from the Calendar in the edit text. Problem is that, only when I click on the EditText for the second time then the calendar open. Please help me to resolve the issue(why calendar don't open for the first time).

的EditText XML code

EditText XML code

<EditText
            android:id="@+id/dateofBirth"
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:maxLines="1"
            android:hint="dd/mm/yyyy" />

活动 code

public void informationPopUp() {
        final Dialog dialog= new Dialog(MainActivity.this,R.style.Dialog_Fullscreen);
        dialog.setContentView(R.layout.details_dialog); 
        dateofBirth = (EditText)dialog.findViewById(R.id.dateofBirth);

        dialog.show();
         myCalendar = Calendar.getInstance();

       final 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(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }


        };

        dateofBirth.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);
              }
        });

    }
     private void updateLabel() {
         String myFormat = "MM/dd/yy"; //In which you need put here
         SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
         dateofBirth.setText(sdf.format(myCalendar.getTime()));
     }

     protected Dialog onCreateDialog(int id) {
            final Calendar now = Calendar.getInstance();

            switch (id) {
            case DATE_DIALOG_ID:
                // set date picker as current date
                DatePickerDialog _date =   new DatePickerDialog(this, date,myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)){
                    @Override

                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){   

                        if (year > now.get(Calendar.YEAR))

                            view.updateDate(myCalendar
                                    .get(Calendar.YEAR), myCalendar
                                    .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                        if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
                            view.updateDate(myCalendar
                                    .get(Calendar.YEAR), myCalendar
                                    .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                        if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) && 
                                monthOfYear == now.get(Calendar.MONTH))
                            view.updateDate(myCalendar
                                    .get(Calendar.YEAR), myCalendar
                                    .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
                            }
                };
                return _date;
            }
            return null;
        } 

我不明白我做了什么错。请提出一个解决方案。

I am not understanding what I have done wrong. Please suggest a solution.

推荐答案

我会尽力解决您的问题,但我不是100%肯定第一个原因: - )

I'll try to address your problem, But am not 100% sure about the first reason :-)

1:您压延只有在第二次点击打开的问题:这是因为,你正在使用一个EditText。在第一个点击,你的编辑文本将获得焦点。那么第二次点击仅调用onClickListener。

1: The problem that your calender is opening only in the second click : this is because, you are using an edittext. On the first click, your Edit Text will get focus. then the second click only calls the onClickListener.

如果你不期待编辑日期手动设置(使用键盘),那么为什么不使用一个TextView来显示所选择的日期?

If you are not looking forward to edit the date set manually (using keyboard), then why not using a TextView to display the selected Date?

2:您code不更新EDITTEXT日期的问题。这是因为你没有设置DateSetListener在code。您需要设置通知一个日期定系统。该DateChange听者只有当您要更改日期返回日期。

2: The problem that your code doesn't update the date in editText. This is because you are not setting the DateSetListener in your code. you need to set that to notify the system that a Date was set. The DateChange listner only returns the date while you are changing the date.

和不设置在EditText上的日期在code(ATLEAST,我没发现它: - )

And you are not setting the date in the EditText in the code (atleast, I didn't found it :-)

试试这个code:

Calendar cal = Calendar.getInstance(TimeZone.getDefault());
                DatePickerDialog datePicker = new DatePickerDialog(this,
                        R.style.AppBlackTheme, datePickerListener,
                        cal.get(Calendar.YEAR), 
                                            cal.get(Calendar.MONTH),
                        cal.get(Calendar.DAY_OF_MONTH));
                datePicker.setCancelable(false);
                datePicker.setTitle("Select the date");

                return datePicker;
            }
        } catch (Exception e) {
            showMsgDialog("Exception",
                    "An error occured while showing Date Picker\n\n"
                            + " Error Details:\n" + e.toString(), "OK");
        }
        return null;
    }


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

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            String year1 = String.valueOf(selectedYear);
            String month1 = String.valueOf(selectedMonth + 1);
            String day1 = String.valueOf(selectedDay);
            TextView tvDt = (TextView) findViewById(R.id.tvDate);
            tvDt.setText(day1 + "/" + month1 + "/" + year1);

        }
    };

在此,我更新的日期与身份证tvDate一个TextView。我建议你​​使用一个TextView,而不是EditText上,并尝试这个code。

In this, I am updating the date to a TextView with id "tvDate". I suggest you to use a TextView instead of EditText and try this code.

如果你是如此特别是在使用EDITTEXT,你需要加载在第一压延单击本身,然后尝试设置一个onFocusListner到EDITTEXT,而不是onClickListner。

If you are so particular in using Edittext, and you need to load the calender in first click itself, then try setting an onFocusListner to the editText instead of onClickListner.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus)
        {
           //Show your calender here 
        }else{
           //Hide your calender here
        }
    }
});

注:Voteup和放大器;接受我的答案,如果它可以帮助你的呢。

NB: Voteup & accept my answer if it helps you in anyway.

这篇关于在点击的EditText的打开DatePickerDialog需要两次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆