![](/img/trans.png)
[英]How to go to next input by pressing enter in soft keyboard in android web-view phonegap build
[英]Android Keyboard Replacement Web-view
是否可以將網絡視圖嵌入到Android鍵盤替換應用程序中?
我有一個用javascript編寫的界面,並希望將其嵌入到Android鍵盤替換應用程序中。 它需要能夠檢測觸摸事件,並從Web視圖將文本字符串發送回本機UI。
我已經搜索了谷歌這個,但無法找到有關如何在鍵盤替換應用程序中創建Web視圖的任何信息。
https://github.com/billymoon/javascript-android-keyboard-boilerplate
是。 簡而言之。 不是一個好主意,但技術上可行。 讓我向您介紹一個快速示例項目,該項目改編自android SDK附帶的SoftKeyboard示例。 現在還有許多其他技術問題需要解決,但這應該為您提供一個基本的起點....
首先,讓我們創建一個基本布局作為我們的鍵盤使用:
\\水庫\\布局\\ 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>
這是android要求接受我們的IME
\\水庫\\ XML \\ method.xml:
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android" />
現在進入我們的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>
很明顯, uses-sdk
和user-permission
是由應用程序決定的,而不是這個代碼所要求的(我這里沒有使用任何互聯網文件,但你可以,我測試了它並且它有效...)
現在定義一個簡單的鍵盤\\ 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() {}
}
這里我們基本上創建一個webview,然后從資產文件填充它,並在啟用javascript后綁定一個簡單的界面
這是資產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>
就是這樣,運行它,你會得到一個帶有單個按鈕的鍵盤,當你按下它時,javascript會將文本發送到輸入作曲家......
希望有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.