Android 在 DatePicker 中选取日期后无法获取 EditText id

Android can't get EditText id after Date is picked in DatePicker

我的 android 表单中有 3 个 EditText 字段,每个字段在单击后都显示日历日期选择器。这很好用。

 sowin =(EditText) findViewById(R.id.sowin);
        sowout =(EditText) findViewById(R.id.sowout);
        ripe =(EditText) findViewById(R.id.ripe);

        DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH,month);
                myCalendar.set(Calendar.DAY_OF_MONTH,day);

                EditText ide= findViewById(buttonClicked);
                updateLabel(ide);
            }
        };


        sowin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int buttonClicked = view.getId();
                new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        sowout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int buttonClicked = view.getId();
                new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        ripe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int buttonClicked = view.getId();
                new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    private void updateLabel(EditText ide){
        String myFormat="dd.MM.yy";
        SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
        ide.setText(dateFormat.format(myCalendar.getTime()));
    }

但我想将选择的日期设置到正确的字段,从日期选择器被初始化的地方。

这就是为什么我尝试添加到每个 Click 事件 int buttonClicked = sowin.getId(); 以获取单击字段的 ID,然后在 onDateSet 方法中

EditText ide= findViewById(buttonClicked);
updateLabel(ide);

但是我有一个 Nullpointerexception,因为在 updateLabel 方法中尝试 ide.setText 时字段的 ID 未知。

如何从初始化日期选择器的位置获取正确的字段 ID 以正确写入它的值?

您正在为 onDateSetListener 中无法访问的每个 onclick 方法创建新的局部变量,因此将 buttonClicked 设置为全局变量,并从每个 onclick 方法中对其进行初始化。

 **int buttonClicked** //Global variable
    
    sowin =(EditText) findViewById(R.id.sowin);
            sowout =(EditText) findViewById(R.id.sowout);
            ripe =(EditText) findViewById(R.id.ripe);
    
            DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH,month);
                    myCalendar.set(Calendar.DAY_OF_MONTH,day);
    
                    EditText ide= findViewById(buttonClicked);
                    updateLabel(ide);
                }
            };
    
    
            sowin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    **buttonClicked = view.getId();**    //just initialize here before opening datepicker 
                    new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    
            sowout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    **buttonClicked = view.getId();**    //just initialize here before opening datepicker 
                    new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    
            ripe.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    **buttonClicked = view.getId();**   //just initialize here before opening datepicker 
                    new DatePickerDialog(AddVysev.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    
        private void updateLabel(EditText ide){
            String myFormat="dd.MM.yy";
            SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
            ide.setText(dateFormat.format(myCalendar.getTime()));
        }