EditText上的DatePickerDialog单击“事件”

时间:2017-07-19 13:50:35

标签: android datepicker

我的应用程序中有多个DatePickers,我试图单个方法为多个TextView设置datepicker而不是将Datepicker放到每个TextView,Date对话框似乎正确显示但更改只更新到最后一个项目

这是我创建的方法

public void Setdate( final EditText setdateEditview){

        setdateEditview.setInputType(InputType.TYPE_NULL);
        setdateEditview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog.show();
                Log.d ("ttt","herre we o"+setdateEditview);
            }


        });

        Calendar newCalendar = Calendar.getInstance();
        DatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            @RequiresApi(api = Build.VERSION_CODES.N)
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                Log.d ("ttt","herre we o"+setdateEditview);
                setdateEditview.setText(dateFormatter.format(newDate.getTime()));
            }

        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    }

我正在通过此方式调用上述方法....

mCustomerDueDateEditText = (EditText) findViewById(R.id.edit_CustommerDueDate);
mForecastedDueDateEditText = (EditText) findViewById(R.id.edit_ForcastedDueDate);
Setdate(mCustomerDueDateEditText);
Setdate(mForecastedDueDateEditText);

1 个答案:

答案 0 :(得分:0)

像这样更改方法签名,并在想要更新时循环遍历它。

   public void Setdate( final EditText... setdateEditviewList){

                setdateEditview.setInputType(InputType.TYPE_NULL);
                setdateEditview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        DatePickerDialog.show();
                        Log.d ("ttt","herre we o"+setdateEditview);
                    }


                });

                Calendar newCalendar = Calendar.getInstance();
                DatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

                    @RequiresApi(api = Build.VERSION_CODES.N)
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Calendar newDate = Calendar.getInstance();
                        newDate.set(year, monthOfYear, dayOfMonth);
                        Log.d ("ttt","herre we o"+setdateEditview);

               //loop through the EditText here
                 EditText et;
                for(int = 0; i<setdateEditviewList.length; i++){
                          et = setdateEditviewList[i];
                          et.setText(dateFormatter.format(newDate.getTime()));

           }
                    }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

            }

按如下方式调用方法:

mCustomerDueDateEditText = (EditText) findViewById(R.id.edit_CustommerDueDate);
mForecastedDueDateEditText = (EditText) findViewById(R.id.edit_ForcastedDueDate);
Setdate(mCustomerDueDateEditText, mForecastedDueDateEditText);