无法在Android应用的日期选择器中设置值

时间:2018-01-10 07:08:33

标签: android datepicker

以下是为日期选择器写入设置值的代码

 List<WebElement> datePickerBox = driver.findElements(By.xpath("//*
    [@class='android.widget.EditText']"));

    WebElement day = datePickerBox.get(0);
    WebElement month = datePickerBox.get(1);
    WebElement year = datePickerBox.get(2);

    day.sendKeys("05");
    System.out.println("xx : " + day.getText());
    System.out.println("xx : " + month.getText());
    System.out.println("xx : " + year.getText());

1 个答案:

答案 0 :(得分:0)

第一次看到这种方法使用日期选择器。所以我无法帮助您解决问题,但我可以提供我用于日期选择器的方法(用于移动应用程序):

在您的活动中定义这些方法和这些侦听器:

    EditText dateE = findViewById(R.id.date_edit_text);
    // listener which will not open(close) the keyboard when field has focus
    // so on click will be opened just datepicker dialog.
    dateE.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus)
                showDialog(999);
            keyboardCloseListener.closeKeyboard(view);
            dateE.clearFocus();
        }
    });
    // (optional) on click open datepicker dialog
    dateE.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showDialog(999);
        }
    });

然后在同一个活动中创建初始化日期集听众:

    // the listener defines what to do after date is selected.
    private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int month, int day) {
            // after date select setting date to edit text as text.
            dateE.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
        }
    };

最后创建编辑文本对话框的方法点击:

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == 999) {
            int year = 1990;
            int month = 1;
            int day = 1;
            return new DatePickerDialog(this,
                    myDateListener, year, month, day);
        }
        return new DatePickerDialog(this,
                myDateListener, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
    }

希望它能以某种方式帮助你......