问题
I am customizing DatePickerDialog to add some of my functionality for native DatePicker. that is working as expected. But day, month and year fields of DatepickerDialog are editable by default. so when i focus those editable input fields soft keyboard will be open by default and used will be able to edit the date/month/year and when user edit the day/month/year ans press set/cancel edited date DatePickerDialog is getting closed and functionality is working properly. Here my issue is when user edit the day/month/year ans press set/cancel edited date DatePickerDialog is getting closed and softkeyboard is still opened. its not getting closed as soon as DatePickerDialog is dismissed. so for that i tried to hide the softkeyboard with the following code.
private DialogInterface.OnDismissListener myOnDismissListener = new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) ((Activity) ctContext)
.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
};
But this is not working. So I tried to toggle the softkeyboard with the following code.
private DialogInterface.OnDismissListener myOnDismissListener = new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) ((Activity) ctContext)
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if(imm.isActive())
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
};
But here imm.isActive() is always returning false. if i remove that if(imm.isActive()) condition its working when softkeyboard is open. but when softkeyboard is not opened and used just opened DatePickerDialog and set or cancel the date by changing with arrows softkeyboard is getting opened on dismiss of DatePickerDialog. So i need to get that imm.isActive() value properly. but its always returning false.
So some one please help me to get the proper way to decide whether softkeyboard is opened or not.
回答1:
This is the best option which stops manual editing in datepickerdialog. You can disable the softkeyboard using this.
final DatePickerDialog dp = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
txtdate.setText(dateFormatter.format(newDate.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
dp.show();
dp.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
回答2:
Here's the way I've handled force closing the soft keyboard
@Override
public void dismiss() {
super.dismiss();
//hide the soft keyboard
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
回答3:
Best option for stopping manual editing in DatePickerDialog items is using set the descendantFocusability . You can add it in code or in your dialog style file. Personally I recommended to use it in style.
Method 1 : Using Code
datePickerDialog.datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS)
Method 2 : Using Style
<style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog"> <item name="android:descendantFocusability">blocksDescendants</item </style>
Now set the style in dialog
datePickerDialog = DatePickerDialog(
this.context,
R.style.MyDatePickerDialogTheme
)
I think you get the idea. Happy codig.
来源:https://stackoverflow.com/questions/13796634/how-to-hide-the-softketboard-when-datepickerdialog-is-open-on-the-screen