Android WebView 焦点问题 [英] Android WebView focus problem

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

问题描述

我正在与 WebView 的焦点管理作斗争:WebView 与经典组件的焦点管理混乱.这是一个简单的应用程序,其中只有一个 EditBox 和一个加载 Google 的 WebView!

I am fighting with focus management of WebView: WebView messes with focus management of classic components. Here is a simple application with just an EditBox and a WebView in it which loads Google!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:and="http://schemas.android.com/apk/res/android"
              xmlns:webtag="http://schemas.android.com/apk/res/com.webtag"
              and:orientation="vertical" and:layout_width="fill_parent" and:layout_height="fill_parent">
    <LinearLayout and:id="@+id/HorizontalScrollView02"
                  and:layout_width="fill_parent" and:layout_height="wrap_content">
        <EditText and:id="@+id/EditText01"
                  and:layout_width="wrap_content" and:layout_height="wrap_content"
                  and:text="@+id/EditText01"/>
    </LinearLayout>
    <WebView and:id="@+id/uiContent"
             and:layout_weight="1"
             and:layout_width="fill_parent"
             and:layout_height="fill_parent"/>
</LinearLayout>

这是Java代码:

public class WebtagActivity extends Activity
{
    // UI components.
    private WebView mContent;
    private EditText mUrl;


    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mContent = (WebView) findViewById(R.id.uiContent);
        mContent.getSettings().setJavaScriptEnabled(true);
        mContent.loadUrl("http://www.google.fr/");
    }
}

基于这个非常简单的例子,不可能将注意力集中在 WebView(即 google 搜索词输入框)上!但是当添加mContent.requestFocus();"时在初始化期间,情况会好转.如果我首先单击 WebView,我可以从我的 EditText 切换到我的 WebView……但是行为很快就会变得非常不稳定",WebView 有时在触摸它时没有获得焦点,有时却在 WebView 字段周围保持焦点矩形(橙色或绿色,取决于您的手机)再次触摸返回 EditText 组件(我可以在其中写入文本)!

Based on this very simple example, it's impossible to get focus on the WebView (i.e. the google search terms input box)! However when adding "mContent.requestFocus();" during initialization, things get better. I can switch from my EditText to my WebView if I click on WebView first only... But behaviour gets soon very "erratic", with WebView sometimes not getting focus when touching it, sometimes getting it but staying with a focus rectangle around WebView fields (orange or green depending on your phone) after another touch to go back on EditText component (in which I can write text)!

无论如何,我发现的一个解决方案是添加一个 touchListener 并在单击 WebView 时请求焦点:

Anyway, a solution I found is to add a touchListener and request focus when clicking on a WebView:

mContent.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; 
            }
    });

这解决了几乎所有的问题(从经典组件切换到 WebView 和编写文本),除了一个:WebView 将 focusRectangle(绿色或橙色)保持在字段周围,无论它是否有焦点.在下面的屏幕截图中,我的 EditText 具有焦点并接收文本,但 WebView 看起来仍然具有焦点.我试过 clearFocus 但这没有任何作用:

This solves almost all problems (switching from classic components to WebView and writing text) except one: WebView keeps the focusRectangle (green or orange) around fields whether it has focus or not. In the below screenshot, my EditText has the focus and receive text but WebView still looks like it has focus. I tried clearFocus but that doesn't do anything:

查看结果

知道如何解决这个问题吗?非常感谢您的帮助!

Any idea how to solve this? Thanks a lot for your help!

推荐答案

只需将 android:focusable="true" 添加到您的 WebView 布局:

Just add android:focusable="true" to your WebView layout:

<WebView android:id="@+id/uiContent"
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"/>

这篇关于Android WebView 焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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