开发者

Android WebView focus problem

开发者 https://www.devze.com 2023-01-27 01:01 出处:网络
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!

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/u开发者_如何学GoiContent"
             and:layout_weight="1"
             and:layout_width="fill_parent"
             and:layout_height="fill_parent"/>
</LinearLayout>

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/");
    }
}

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)!

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

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:

See the result

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


make sure that parent layout that extends "ViewGroup" doesn't have property

android:descendantFocusability = "blocksDescendants" which prevent child layout from getting focused


This helped my problem for focus:

Webview.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);

and this for sensitive touch:

Webview.getSettings().setLightTouchEnabled(true);


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"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消