0

我试图用 webview 覆盖 android 应用程序的输入;它进入我的onKeyDown,因为我得到了日志,但它没有做任何我尝试过的所有事情,

也许是因为它是一个 webview?, webview 是在事件发生后加载的?

这是我的代码:

public class MainActivity extends Activity{


private WebView mWebView;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    //asociamos
    mWebView = (WebView) findViewById(R.id.webview);

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebView.setFocusable(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.setScrollbarFadingEnabled(false);

    //cargo un .html que he situado en la carpeta asset/ del proyecto
    mWebView.loadUrl("localUrl webview");
    mWebView.requestFocusFromTouch();
    mWebView.requestFocus();


}


public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.v("keyEntra",String.valueOf(keyCode));
    mWebView.requestFocus();
      int mappedKey = keyCode;
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        mappedKey = KeyEvent.KEYCODE_DPAD_LEFT;
         super.onKeyDown(mappedKey, event);
         return true;
    }
    else if(keyCode == KeyEvent.KEYCODE_BACK){
      mappedKey = KeyEvent.KEYCODE_A;
    Log.v("keyNew",String.valueOf(mappedKey));
     super.onKeyDown(mappedKey, event);
     return true;
    }
    else{
        return super.onKeyDown(keyCode, event);  
    }

}

谢谢,

4

1 回答 1

0

好的,我终于找到了解决方案,只需使用仪器

    public static void simulateKey(final int KeyCode) {

    new Thread() {
        @Override
        public void run() {
            try {
                Instrumentation inst = new Instrumentation();
                inst.sendKeyDownUpSync(KeyCode);
            } catch (Exception e) {
                Log.e("Exception when sendKeyDownUpSync", e.toString());
            }
        }

    }.start();
}
于 2013-10-01T08:16:41.773 回答