android - 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 打开时完美运行,但问题是用户需要点击两次才能打开它。我哪里做错了?如何修复代码?
解决方案
将focusable和focusableInTouchMode值更改为 false,它会成功的
android:focusable="false"
android:focusableInTouchMode="false"
希望这会有所帮助!
推荐阅读
- .htaccess - 如何使用 htaccess 运行具有特定 url 的页面
- reactjs - ReactJS 将文本字段限制为带有 2 个小数位的数字
- javascript - 状态改变时如何改变图标?
- objective-c - 如何在objective c项目中使用swift pod而不在pod类中手动编写@objc?
- python - 如何拆分熊猫中的列值?
- python - 查找连续出现 N 次以上的特定值的最大索引
- javascript - canvas.drawImage 裁剪图像
- sql-server - 无法通过powershell连接到RDS SQL Server
- json - 如何根据传递的 eventType 的值在响应的 Json 中具有动态属性名称
- progressive-web-apps - 如何使用 Laravel Mix Workbox 使网站离线可用