Har*_*ish 7 android key input webview dispatch
我需要以编程方式为webview输入控件提供输入.为此,我使用的是webView.dispatchKeyEvent(),它在Android 4.3版本之前工作正常,但它不适用于4.4版本(Kitkat - Chromium webView).
我在logcat中看到以下语句:
W/UnimplementedWebViewApi(9737): Unimplemented WebView method onKeyMultiple called from: android.webkit.WebView.onKeyMultiple(WebView.java:2179)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了dispatchKeyEvent(),onKeyDown()但是4.4中的Chromium webView没有任何工作,如果有办法以编程方式向webView字段发送输入,请有人告诉我.
请注意,我正在寻找任何网页的通用解决方案(例如:Facebook URL中的用户名和密码字段),我不知道输入控件的名称/ ID,因此无法使用简单的Javascript方法来加载输入.
您可以Instrumentation为此使用类。我已经在Android 4.4上对其进行了测试,并且可以正常工作。
首先确保WebView具有焦点,然后调用sendCharacterSync()发送单个键事件。请注意,这些调用必须从后台线程完成(这是必需的)。
例如:
final Instrumentation instrumentation = new Instrumentation();
new Thread(new Runnable()
{
@Override
public void run()
{
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_H);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_I);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_TAB);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_T);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_H);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_E);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_R);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_E);
}
}).start();
Run Code Online (Sandbox Code Playgroud)
唯一的问题是您会获得“打字机”效果(即字母一个接一个地出现,而不是一次全部出现)。
| 归档时间: |
|
| 查看次数: |
1291 次 |
| 最近记录: |