출처: http://stackoverflow.com/questions/4293965/android-webview-focus-problem
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>
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:
///////////////////////////////////////////////////////////////////
출처: http://www.androidpub.com/721579
'구글과인터넷 > 안드로이드' 카테고리의 다른 글
안드로이드|Android WebViewClient 사용하기 (0) | 2012.12.12 |
---|---|
안드로이드 포커스를 가주가는 컨트롤 문제 일시적 해결방법 - 리스트의 버튼이나 체크박스, 에디트 박스 경우 (0) | 2012.12.12 |
안드로이드 - 기본 포커스 주황색 네모 포커스 비슷하게, xml로 따라해서 그리기(drawable) (0) | 2012.12.12 |
WebView에 OnFocusChanged() 리스너를 구현해서 WebView가 focus를 얻으면
단 해당 EditText의 Selector 로 default, focus, pressed 등의 상태 정보가 있어야 에러없이 사용합니다.