[英]Android WebView focus problem
I am fighting with focus management of WebView: WebView messes with focus management of classic components. 我正在与WebView的焦点管理作斗争:WebView混淆了经典组件的焦点管理。 Here is a simple application with just an EditBox and a WebView in it which loads Google!
这是一个简单的应用程序,只有一个EditBox和一个WebView加载谷歌!
<?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>
And here is the Java code: 这是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/");
}
}
Based on this very simple example, it's impossible to get focus on the WebView (ie the google search terms input box)! 基于这个非常简单的例子,不可能专注于WebView(即谷歌搜索术语输入框)! However when adding "mContent.requestFocus();"
但是在添加“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)!
如果我先点击WebView,我可以从我的EditText切换到我的WebView ......但是行为很快变得非常“不稳定”,WebView有时在触摸时无法获得焦点,有时会得到它但是在WebView字段周围保持焦点矩形(橙色或绿色取决于您的手机)在另一次触摸之后返回EditText组件(我可以在其中写入文本)!
Anyway, a solution I found is to add a touchListener and request focus when clicking on a WebView: 无论如何,我找到的解决方案是在单击WebView时添加touchListener并请求焦点:
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. 这解决了几乎所有问题(从经典组件切换到WebView和编写文本),除了一个:WebView将focusRectangle(绿色或橙色)保持在字段周围,无论它是否具有焦点。 In the below screenshot, my EditText has the focus and receive text but WebView still looks like it has focus.
在下面的屏幕截图中,我的EditText具有焦点和接收文本,但WebView看起来仍然具有焦点。 I tried clearFocus but that doesn't do anything:
我尝试过clearFocus但是没有做任何事情:
Any idea how to solve this? 不知道怎么解决这个问题? Thanks a lot for your help!
非常感谢你的帮助!
make sure that parent layout that extends " ViewGroup
" doesn't have property 确保扩展“
ViewGroup
”的父布局没有属性
android:descendantFocusability = "blocksDescendants"
which prevent child layout from getting focused android:descendantFocusability = "blocksDescendants"
阻止子布局聚焦
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: 只需将android:focusable =“true”添加到WebView布局:
<WebView android:id="@+id/uiContent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.