android-无法将文本插入选项卡上的webview

android-无法将文本插入选项卡上的webview,第1张

概述我有两个标签,每个标签都包含一个Webview(为简单起见,带有google页面).Web视图加载正常,但是当我尝试在Google的搜索字段中插入一些文本时,软键盘会按预期显示,但是如果我开始编写一些文本,则该字段会失去焦点.似乎它认为软键盘是选项卡布局的一部分,而不是网页布局的一部分.我已

我有两个标签,每个标签都包含一个WebvIEw(为简单起见,带有Google页面).
Web视图加载正常,但是当我尝试在Google的搜索字段中插入一些文本时,软键盘会按预期显示,但是如果我开始编写一些文本,则该字段会失去焦点.
似乎它认为软键盘是选项卡布局的一部分,而不是网页布局的一部分.

我已经花了数小时尝试所有找到的解决方案,但是其中任何一个都可以解决我的问题.
例如:Why is Android WebView refusing user input?和Can’t type inside a Web View

任何其他想法真的很受欢迎!
我认为所有相关代码都在下面发布,否则请询问.

谢谢!

MainActivity.java(FragmentActivity)

public voID onTabChanged(String selected) {Fragment shownFragment = fm.findFragmentByTag(shown);FragmentTransaction ft = fm.beginTransaction();// Remove the current fragment from screenif (shownFragment != null) {    ft.remove(shownFragment);}// Change to the new fragmentif (selected.equals("Tab1")) {    shown = "Tab1";    ft.add(R.ID.fragment_content, new WebFragment(), "Tab1");} else if (selected.equals("Web")) {    shown = "Web";    ft.add(R.ID.fragment_content, new WebFragment(), "Web");}ft.commit();}

WebFragment.java

public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,    Bundle savedInstanceState) {webVIEw = (WebVIEw) inflater.inflate(R.layout.activity_web_fragment, container, false);webVIEw.getSettings().setJavaScriptEnabled(true);webVIEw.loadUrl("http://www.Google.com");webVIEw.setWebVIEwClIEnt(new MyWebVIEwClIEnt());Log.v("WebFragment", "Returning web vIEw");webVIEw.requestFocus(VIEw.FOCUS_DOWN);return webVIEw;}private class MyWebVIEwClIEnt extends WebVIEwClIEnt {@OverrIDepublic boolean shouldOverrIDeUrlLoading(WebVIEw vIEw, String url) {    vIEw.loadUrl(url);    return true;}@OverrIDepublic boolean shouldOverrIDeKeyEvent(WebVIEw vIEw, KeyEvent event) {    if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && vIEw.canGoBack()) {    vIEw.goBack();    return true;    }    return super.shouldOverrIDeKeyEvent(vIEw, event);}}

activity_main.xml

<linearLayout androID:ID="@+ID/linearLayout1" androID:orIEntation="vertical" xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent"androID:layout_height="match_parent"><TabHost androID:ID="@androID:ID/tabhost"     androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"     androID:layout_margintop="2dp">    <linearLayout androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"         androID:orIEntation="vertical">        <horizontalscrollview androID:ID="@+ID/hScrollVIEw"             androID:layout_wIDth="fill_parent"             androID:layout_height="wrap_content"             androID:fillVIEwport="true"             androID:scrollbars="none">            <TabWidget androID:ID="@androID:ID/tabs"                androID:layout_wIDth="wrap_content"                 androID:layout_height="wrap_content"                androID:tabStripEnabled="true"/>        </horizontalscrollview>        <FrameLayout androID:ID="@androID:ID/tabcontent"            androID:layout_wIDth="0dp"             androID:layout_height="0dp"            androID:layout_weight="0" />        <FrameLayout androID:ID="@+ID/fragment_content"            androID:layout_wIDth="match_parent"             androID:layout_height="0dp"            androID:layout_weight="1" />    </linearLayout></TabHost>

activity_web_fragment.xml

<?xml version="1.0" enCoding="utf-8"?><WebVIEw androID:ID="@+ID/webVIEw" xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" />

解决方法:

您好Safime,
   我想我找到了解决方案.我敢肯定,您已经搜索过,并且看到互联网上有此变通方法,声称可以解决该错误:

webvIEw.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.setontouchListener(new VIEw.OntouchListener() {        @OverrIDe        public boolean ontouch(VIEw v, MotionEvent event) {            switch (event.getAction()) {                case MotionEvent.ACTION_DOWN:                case MotionEvent.ACTION_UP:                    //The key is this line.                     v.requestFocusFromtouch();                      break;            }                           return false;        }    });
总结

以上是内存溢出为你收集整理的android-无法将文本插入选项卡上的webview全部内容,希望文章能够帮你解决android-无法将文本插入选项卡上的webview所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1074518.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存