Android 单次点击时未显示日期选择器对话框
期望值:Android 单次点击时未显示日期选择器对话框,android,Android,期望值: public static void hideKeyboard(Activity activity) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); View view = activity.getCurrentFocus(); if (view == null) { view = n
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'
...
}
当用户点击MaterialTextView组件时,DatePickerDialog应该打开
发生了什么事:
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'
...
}
用户需要在MaterialTextView组件上点击两次以打开DatePickerDialog
main活动
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'
...
}
布局:
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"
希望这会有帮助 将您的文本视图更改为
<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:hint="@string/txtVIssueingDtHint"
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" />
删除此选项,除非它是必需的选项
android:focusable="auto"
android:focusableInTouchMode="true"
移动
hideKeyboard(AddNewCoupon.this)代码>到块的末尾,即datePickerDialog.show()之后代码>,我认为您的问题与焦点转移问题有关。我运行了您的代码,一次单击就打开了对话框,因此问题可能与您尚未共享的代码有关。仅供参考,android:onClick=“showDatePickerDialogForIssue”
是多余的,因为您实现了setonclicklistener您的解决方案对我来说很有效。现在我还有一个问题。当用户单击“下一步”按钮时,控件将跳过此组件,直接转到DatePicker旁边的组件,这是一个糟糕的用户体验,如何管理焦点转移。我如何管理焦点转移?