如何设置键盘焦点的WebView? [英] How to set keyboard focus on WebView?

查看:284
本文介绍了如何设置键盘焦点的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下布局的简单的WebView示例应用程序:

I have a simple WebView example app that has the following layout:

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

  <EditText android:id="@+id/urlToLoad"
    android:hint="Type url to load"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/webviewgo"
    android:text="Go"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false" />

  <com.example.exWebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  />    

</LinearLayout>

这正常工作,但我感兴趣的路由键盘事件到的WebView 。目前,即使我选择的WebView(和滚动等)​​,当我输入任何按键,它去的EditText控制......这是不是我想要的。

It works as expected, but I am interested in routing a keyboard event to the WebView. Currently, even if I select the WebView (and scroll etc.), when I type any key, it goes to the EditText control... which is not what I want.

我如何做一个键盘事件进入的WebView呢?

How do I make a keyboard event go to the WebView instead?

推荐答案

根据这个线程,你所要做的就是设置,主要活动的OnCreate(),一个OnTouchListener()与requestFocus()方法里:

According to this thread, all you have to do is set, inside the main activity's OnCreate(), an OnTouchListener() with a requestFocus():

    mWebView = (MyWebView) findViewById(R.id.webview);
    mWebView.setOnTouchListener(new View.OnTouchListener() { 
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) { 
                       case MotionEvent.ACTION_DOWN: 
                       case MotionEvent.ACTION_UP: 
                           if (!v.hasFocus()) { 
                               v.requestFocus(); 
                           } 
                           break; 
                   } 
                   return false; 
                }
        });

这是什么code基本上做的是锁定重点任何向上或向下事件到达您的视图的时刻。注意它是如何返回false,以便事件可以进一步传播到其他的意见,因为如果它不处理的。

What this code basically does is lock focus on any down or up event the moment it reaches your view. Note how it returns false, so that the event can further propagate to other views, as if it weren't handled.

这篇关于如何设置键盘焦点的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆