在 WebView 中禁用 SoftKeyboard
Disable SoftKeyboard inside a WebView
我知道这是一个常见问题,但我无法在此处和 Google 上找到有效的解决方案。
我的 android 应用程序中有一个 WebView,里面有一个 JSF 页面和一个简单的 DatePicker 组件:
<p:calendar value="#{patientHealthDataView.dateFrom}" id="popupButtonDateFrom" showOn="button" class="tableCalendarText"/>
显示带有日历的弹出窗口。
感谢这个命令,我设法使弹出窗口正常工作:
WebView view=(WebView)findViewById(R.id.statistics);
view.getSettings().setJavaScriptEnabled(true);
不幸的是,当我单击输入文本或按钮以显示日历的弹出窗口时,焦点转到输入文本并且 Android 显示隐藏日历的键盘。
我已经尝试了几种解决方案。最后一个是这样的:
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(v);
return false;
}
});
public void hideSoftKeyboard(View v) {
Activity activity = (Activity) v.getContext();
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
根据 Android Studio 的建议,此代码引发空指针异常:
activity.getCurrentFocus().getWindowToken()
其他解决方案说要在清单中插入一个特殊命令,但我只想在 WebView 中阻止键盘。
有什么建议吗?
在 layout.xml
的主(父)布局中添加以下代码
android:descendantFocusability="blocksDescendants"
希望它能奏效。
并在您的网络视图
中设置以下 属性
android:focusable="false"
android:focusableInTouchMode="true"
我遇到了类似的问题。我正在使用带有日期时间选择器的文本输入:
HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" required
JQuery:
$('#some_id').datetimepicker({format: \'YYYY-MM-DD\'});
然而,这会导致问题,因为当您尝试 select 约会时,软键盘会弹出。然后我尝试用下面的方法修复它:
HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" onfocus="blur();" 需要
这可行,但实际打开日历花费的时间太长,但至少没有弹出软键盘。这只是为了向您展示我最初的尝试。
我的解决方案:
输入 type="date" placeholder="Birth Date" id="some_id" name="some_id" value="" required
我所要做的就是将输入类型从 "text" 更改为 "date" 并删除 jquery 行。
希望这对某人有所帮助。 :)
我知道这是一个常见问题,但我无法在此处和 Google 上找到有效的解决方案。
我的 android 应用程序中有一个 WebView,里面有一个 JSF 页面和一个简单的 DatePicker 组件:
<p:calendar value="#{patientHealthDataView.dateFrom}" id="popupButtonDateFrom" showOn="button" class="tableCalendarText"/>
显示带有日历的弹出窗口。
感谢这个命令,我设法使弹出窗口正常工作:
WebView view=(WebView)findViewById(R.id.statistics);
view.getSettings().setJavaScriptEnabled(true);
不幸的是,当我单击输入文本或按钮以显示日历的弹出窗口时,焦点转到输入文本并且 Android 显示隐藏日历的键盘。
我已经尝试了几种解决方案。最后一个是这样的:
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(v);
return false;
}
});
public void hideSoftKeyboard(View v) {
Activity activity = (Activity) v.getContext();
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
根据 Android Studio 的建议,此代码引发空指针异常:
activity.getCurrentFocus().getWindowToken()
其他解决方案说要在清单中插入一个特殊命令,但我只想在 WebView 中阻止键盘。
有什么建议吗?
在 layout.xml
的主(父)布局中添加以下代码android:descendantFocusability="blocksDescendants"
希望它能奏效。
并在您的网络视图
中设置以下 属性android:focusable="false"
android:focusableInTouchMode="true"
我遇到了类似的问题。我正在使用带有日期时间选择器的文本输入:
HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" required
JQuery: $('#some_id').datetimepicker({format: \'YYYY-MM-DD\'});
然而,这会导致问题,因为当您尝试 select 约会时,软键盘会弹出。然后我尝试用下面的方法修复它:
HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" onfocus="blur();" 需要
这可行,但实际打开日历花费的时间太长,但至少没有弹出软键盘。这只是为了向您展示我最初的尝试。
我的解决方案: 输入 type="date" placeholder="Birth Date" id="some_id" name="some_id" value="" required
我所要做的就是将输入类型从 "text" 更改为 "date" 并删除 jquery 行。
希望这对某人有所帮助。 :)