如何在 webview 中按下 go 按钮时关闭键盘
How to dismiss keyboard while pressing go button in a webview
我有一个加载网页的 webview(启用 javascript 并且该页面与 activity 之间有一个接口,我用它直接从 webview 打开相机)。
我在该网页中有几个字段,其中一些是数字,一些是纯文本。每个字段都有不同的键盘布局(尽管都有转到按钮)。
目前“开始”按钮没有任何作用。我需要在单击时关闭键盘。
我发现 tutorial 关于如何实现这一点,但不幸的是,由于我有不同的键盘布局,因此该解决方案对我不起作用。当我应用此解决方案时,所有字段都会获得数字键盘布局。
或字母取决于 outAttrs.inputType
到目前为止,这是我的代码:
public class WebViewGo extends WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
BaseInputConnection inputConnection = new BaseInputConnection(this, false);
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
outAttrs.inputType = EditorInfo.TYPE_CLASS_NUMBER;
return inputConnection;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean isDispached = super.dispatchKeyEvent(event);
if(event.getAction() == KeyEvent.ACTION_UP){
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
break;
}
}
return isDispached;
}
}
我可以在保留多个键盘布局的同时使用此解决方案吗,或者是否有其他解决方案可以解决此问题。
提前致谢。
更新
我完全忘了提到我试图删除 onCreateInputConnection 但没有它,dispatchKeyEvent 似乎不起作用。
更新 2
由于某些原因,我设法将具有关闭功能的完成按钮添加到数字字段,但在使用文本字段时无法正常工作
到目前为止,这是我的自定义 webview 代码
public class WebViewGo extends WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
if ((outAttrs.inputType & InputType.TYPE_CLASS_NUMBER) == InputType.TYPE_CLASS_NUMBER)
{
connection = new BaseInputConnection(this, false);
}else{
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT;
}
return connection;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean isDispached = super.dispatchKeyEvent(event);
if(event.getAction() == KeyEvent.ACTION_UP){
Log.d("anton","dispatchKeyEvent="+event.getKeyCode());
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
break;
}
}
return isDispached;
}
}
所以这个解决方案发展了许多步骤,既是原生的又是 angular。
需要创建自定义 webview 来模拟键盘中的输入(操作按钮)以替换为 'Done' 操作
public class WebViewGo 扩展 WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
if((outAttrs.imeOptions & EditorInfo.IME_ACTION_GO)==EditorInfo.IME_ACTION_GO ||
(outAttrs.imeOptions & EditorInfo.IME_ACTION_NONE)==EditorInfo.IME_ACTION_NONE)
{
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
}
return connection;
}
}
需要在您的 activity/fragment
中使用此 webview 而不是常规 webview
创建自定义 javascript 界面
私有 class WebAppInterface {
@JavascriptInterface
public void dismissKeyboard()
{
InputMethodManager imm = (InputMethodManager) TodoItemDetailActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(webview.getWindowToken(), 0);
}
}
将界面添加到 webview
webview.addJavascriptInterface(new WebAppInterface(), "Android");
诀窍是当用户点击进入时他在 webview 中(意味着它聚焦在网页中(angualr)
当用户在此字段中单击 'Done' 时,我需要移除键盘。
onKeyUp(event : any){
if (event.key === 'Enter') {
if (typeof Android !== 'undefined' && Android) {
event.target.value = event.target.value.replace(/\n/g, "");
//console.log(event.target.value);
Android.dismissKeyboard();
}
}
}
我遇到的另一个小问题是,即使我设法关闭键盘,它仍会在文本末尾添加回车键,我需要将其删除,之后很简单,只需调用关闭的本机函数使用 javascript 界面的 activity/fragment 键盘。
我有一个加载网页的 webview(启用 javascript 并且该页面与 activity 之间有一个接口,我用它直接从 webview 打开相机)。 我在该网页中有几个字段,其中一些是数字,一些是纯文本。每个字段都有不同的键盘布局(尽管都有转到按钮)。
目前“开始”按钮没有任何作用。我需要在单击时关闭键盘。
我发现 tutorial 关于如何实现这一点,但不幸的是,由于我有不同的键盘布局,因此该解决方案对我不起作用。当我应用此解决方案时,所有字段都会获得数字键盘布局。 或字母取决于 outAttrs.inputType
到目前为止,这是我的代码:
public class WebViewGo extends WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
BaseInputConnection inputConnection = new BaseInputConnection(this, false);
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
outAttrs.inputType = EditorInfo.TYPE_CLASS_NUMBER;
return inputConnection;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean isDispached = super.dispatchKeyEvent(event);
if(event.getAction() == KeyEvent.ACTION_UP){
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
break;
}
}
return isDispached;
}
}
我可以在保留多个键盘布局的同时使用此解决方案吗,或者是否有其他解决方案可以解决此问题。
提前致谢。
更新
我完全忘了提到我试图删除 onCreateInputConnection 但没有它,dispatchKeyEvent 似乎不起作用。
更新 2
由于某些原因,我设法将具有关闭功能的完成按钮添加到数字字段,但在使用文本字段时无法正常工作
到目前为止,这是我的自定义 webview 代码
public class WebViewGo extends WebView {
public WebViewGo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
if ((outAttrs.inputType & InputType.TYPE_CLASS_NUMBER) == InputType.TYPE_CLASS_NUMBER)
{
connection = new BaseInputConnection(this, false);
}else{
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT;
}
return connection;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean isDispached = super.dispatchKeyEvent(event);
if(event.getAction() == KeyEvent.ACTION_UP){
Log.d("anton","dispatchKeyEvent="+event.getKeyCode());
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
break;
}
}
return isDispached;
}
}
所以这个解决方案发展了许多步骤,既是原生的又是 angular。
需要创建自定义 webview 来模拟键盘中的输入(操作按钮)以替换为 'Done' 操作
public class WebViewGo 扩展 WebView {
public WebViewGo(Context context, AttributeSet attrs) { super(context, attrs); } @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { InputConnection connection = super.onCreateInputConnection(outAttrs); if((outAttrs.imeOptions & EditorInfo.IME_ACTION_GO)==EditorInfo.IME_ACTION_GO || (outAttrs.imeOptions & EditorInfo.IME_ACTION_NONE)==EditorInfo.IME_ACTION_NONE) { outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE; } return connection; }
}
需要在您的 activity/fragment
中使用此 webview 而不是常规 webview创建自定义 javascript 界面
私有 class WebAppInterface {
@JavascriptInterface public void dismissKeyboard() { InputMethodManager imm = (InputMethodManager) TodoItemDetailActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(webview.getWindowToken(), 0); } }
将界面添加到 webview
webview.addJavascriptInterface(new WebAppInterface(), "Android");
诀窍是当用户点击进入时他在 webview 中(意味着它聚焦在网页中(angualr)
当用户在此字段中单击 'Done' 时,我需要移除键盘。
onKeyUp(event : any){
if (event.key === 'Enter') {
if (typeof Android !== 'undefined' && Android) {
event.target.value = event.target.value.replace(/\n/g, "");
//console.log(event.target.value);
Android.dismissKeyboard();
}
}
}
我遇到的另一个小问题是,即使我设法关闭键盘,它仍会在文本末尾添加回车键,我需要将其删除,之后很简单,只需调用关闭的本机函数使用 javascript 界面的 activity/fragment 键盘。