简体   繁体   English

Android键盘替换Web视图

[英]Android Keyboard Replacement Web-view

Is it possible to embed a web-view into an android keyboard replacement app? 是否可以将网络视图嵌入到Android键盘替换应用程序中?

I have an interface written in javascript, and would like to embed that into an android keyboard replacement app. 我有一个用javascript编写的界面,并希望将其嵌入到Android键盘替换应用程序中。 It will need to be able to detect touch events, and send text strings back to the native UI from the web-view. 它需要能够检测触摸事件,并从Web视图将文本字符串发送回本机UI。

I have searched google for this, but can not find any onformation on how to create web-view in keyboard replacement app. 我已经搜索了谷歌这个,但无法找到有关如何在键盘替换应用程序中创建Web视图的任何信息。


EDIT: Created github project for boilerplate based on the on the answer from @ckozl 编辑:根据@ckozl的答案创建了样板的github项目

https://github.com/billymoon/javascript-android-keyboard-boilerplate https://github.com/billymoon/javascript-android-keyboard-boilerplate

Yes. 是。 In short. 简而言之。 Not a great idea but technically feasible. 不是一个好主意,但技术上可行。 Let me walk you through a quick sample project, adapted from the SoftKeyboard sample included with the android SDK. 让我向您介绍一个快速示例项目,该项目改编自android SDK附带的SoftKeyboard示例。 Now there are a number of other technical issues to tackle but this should provide you with a basic starting point.... 现在还有许多其他技术问题需要解决,但这应该为您提供一个基本的起点....

For starters, lets create a basic layout to use as our keyboard: 首先,让我们创建一个基本布局作为我们的键盘使用:

\\res\\layout\\input.xml: \\水库\\布局\\ input.xml中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

    <WebView
      android:id="@+id/myWebView"
      android:layout_width="match_parent"
      android:layout_height="600dp"
    />

</FrameLayout>

this is required by android to accept our IME 这是android要求接受我们的IME

\\res\\xml\\method.xml: \\水库\\ XML \\ method.xml:

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android"  />

now onto our manifest \\AndroidManifest.xml 现在进入我们的manifest \\ AndroidManifest.xml

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.softkeyboard">

    <uses-sdk android:minSdkVersion="13" android:targetSdkVersion="13" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:label="SoftKeyboard">

        <service 
            android:name="SoftKeyboard"
            android:permission="android.permission.BIND_INPUT_METHOD"
        >
            <intent-filter>
                <action android:name="android.view.InputMethod" />
            </intent-filter>

            <meta-data android:name="android.view.im" android:resource="@xml/method" />      
        </service>
    </application>
</manifest>

obviously the uses-sdk and user-permission are up to the app and not required by this code (i'm not using any internet files here, but you could, i tested it and it worked...) 很明显, uses-sdkuser-permission是由应用程序决定的,而不是这个代码所要求的(我这里没有使用任何互联网文件,但你可以,我测试了它并且它有效...)

now define a simple keyboard \\src...\\SoftKeyboard.java: 现在定义一个简单的键盘\\ src ... \\ SoftKeyboard.java:

package com.example.android.softkeyboard;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.KeyboardView;
import android.view.View;
import android.webkit.WebView;

public class SoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private WebView myWebView = null;

    @Override 
    public View onCreateInputView() {

        View view = getLayoutInflater().inflate(R.layout.input, null);
        myWebView = (WebView) view.findViewById(R.id.myWebView);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(jsInterface, "android");

        return view;
    }

    private Object jsInterface = new Object() {

        @SuppressWarnings("unused")
        public void sendKeys() {
            getCurrentInputConnection().setComposingText("how do ya like me now?", 1);
        }

    };

    @Override
    public void onWindowShown() {

        super.onWindowShown();      
        myWebView.loadUrl("file:///android_asset/keyboard_test.html");
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {}

    @Override
    public void onPress(int primaryCode) {}

    @Override
    public void onRelease(int primaryCode) {}

    @Override
    public void onText(CharSequence text) {}

    @Override
    public void swipeDown() {}

    @Override
    public void swipeLeft() {}

    @Override
    public void swipeRight() {}

    @Override
    public void swipeUp() {}

}

here we basically create a webview then populate it from a asset file and bind a simple interface to it after enabling javascript 这里我们基本上创建一个webview,然后从资产文件填充它,并在启用javascript后绑定一个简单的界面

here's the asset html: *\\assets\\keyboard_test.html* 这是资产html: * \\ assets \\ keyboard_test.html *

<!DOCTYPE html>
<html>
<head>
<title>test</title>
<style>
button {

    display:block;
    margin:300px auto;
    width:400px;
    padding:60px;

}
</style>
</head>
<body>

<button onclick="android.sendKeys()">yeah buddy!</button>

</body>
</html>

and that's it, run it and you'll get a keyboard with a single button and when you push it the javascript will send text into the input composer... 就是这样,运行它,你会得到一个带有单个按钮的键盘,当你按下它时,javascript会将文本发送到输入作曲家......

hope that helps -ck 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在Android Web View Phonegap构建中按软键盘中的Enter进入下一个输入 - How to go to next input by pressing enter in soft keyboard in android web-view phonegap build 如何为iOS / Android创建Web视图? - How to create a web-view for iOS/Android? Android Web-View:将本地Javascript文件注入远程网页 - Android Web-View : Inject local Javascript file to Remote Webpage 如何在网络视图内访问Android应用程序的本地文件 - how to access local files of android app inside a web-view Android EPSON热点打印来自网页视图的数据点击?如果找不到打印机? - Android EPSON thermal Print data from web-view on click?If Printer Not Found? 为什么我在vue.js的android网络视图中看到此错误 - Why do I see this error in android web-view in vue.js Android Web 视图错误 I/chromium: [INFO:CONSOLE(1)] &quot;Uncaught ReferenceError: - Android Web-view Error I/chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: 从微信小程序发送消息到网页视图 - Send message from WeChat mini-program to web-view 如何在浏览器而不是Web视图中强制打开链接 - How to forcefully open a link in browser rather than in web-view NativeScript 出现此错误 TypeError:在创建 Web 视图时无法读取未定义的属性“loadData” - NativeScript is giving this error TypeError: Cannot read property 'loadData' of undefined while creating web-view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM