机器人的WebView焦点问题 [英] Android WebView focus problem

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

问题描述

我一起战斗的WebView的重点管理:食堂的WebView经典部件的重点管理。下面是一个简单的应用程序只是一个编辑框和它的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 code:

And here is the Java code:

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(即谷歌搜索词输入框)!然而当添加mContent.requestFocus();在初始化过程中,事情变得更好。我可以从我的EditText到我的WebView切换,如果我第一只的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!

推荐答案

只需添加的机器人:可调焦=真正的的到你的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"/>

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

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