我有一个加载了网页的Web视图(启用了JavaScript,并且该页面与我用来直接从Web视图中打开相机的活动之间存在接口)。 我在该网页中有几个字段,其中一些是数字,一些是纯文本。每个字段都有不同的键盘布局(尽管都具有“执行”按钮)。
当前,“执行”按钮未执行任何操作。我需要在单击时关闭键盘。
我找到了如何实现此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;
}
}
答案 0 :(得分:0)
因此,该解决方案发展了许多本机的和有角度的步骤。
需要创建一个自定义Web视图,以模拟键盘中的Enter(操作按钮)以替换为“完成”操作
公共类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;
}
}
需要在您的活动/片段中使用此Web视图而不是常规Web视图
创建自定义javascript界面
私有类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");
诀窍是,当用户单击Enter时,他位于Web视图中(这意味着它集中在网页上(角度)
当用户在此字段中单击“完成”时,我需要卸下键盘。
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界面从活动/片段中提取。