首页 > 解决方案 > DatePicker 对话框未在单击时显示

问题描述

期待:

当用户点击 MaterialTextView 组件时,DatePickerDialog 应该打开。

发生了什么:

用户需要在 MaterialTextView 组件上点击两次才能打开 DatePickerDialog。

主要活动

protected void onCreate(Bundle SavedInstanceState){
MaterialTextView issueDate = findViewById(R.id.edIssueDate);
 issueDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            hideKeyboard(AddNewCoupon.this);
            DatePickerDialog datePickerDialog = new DatePickerDialog(AddNewCoupon.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    issueDate.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(month)+"/"+String.valueOf(year));
                    //newCustomer.hideKeyboard(AddNewCoupon.this);
                }
            }, year, month, day);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            datePickerDialog.show();
        }
    });
}

hideKeyboard() 方法:

public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view == null) {
        view = new View(activity);
    }

    if (imm != null)
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

布局:

<com.google.android.material.textview.MaterialTextView
                        android:id="@+id/edIssueDate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:ems="10"
                        android:focusable="auto"
                        android:focusableInTouchMode="true"
                        android:hint="@string/txtVIssueingDtHint"
                        android:nextFocusDown="@id/edExpDate"
                        android:onClick="showDatePickerDialogForIssue"
                        android:paddingLeft="4dp"
                        android:textColor="@color/design_default_color_on_secondary"
                        android:textSize="18sp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/textView2" />

依赖项:

dependencies {
...

implementation 'com.google.android.material:material:1.2.0-alpha04'

...
}

代码在 DatePickerDialog 打开时完美运行,但问题是用户需要点击两次才能打开它。我哪里做错了?如何修复代码?

标签: android

解决方案


将focusablefocusableInTouchMode值更改为 false,它会成功的

 android:focusable="false"
 android:focusableInTouchMode="false"

希望这会有所帮助!


推荐阅读