首页 > 解决方案 > 在webview中输入键盘时如何隐藏底部导航栏

问题描述

在一个片段中的 WebView 中键入键盘时,如何隐藏底部导航栏?

这是我的片段:

    public class chatFragment extends Fragment {
        LinearLayout eLinearLayout_chat;
        WebView webView_chat;
        ProgressBar mProgressBar_chat;
        BottomNavigationView nav;
        //SwipeRefreshLayout swipeRefreshLayout_chat;
        // Firebase instance variables
        //private FirebaseAuth mFirebaseAuth;
        //private FirebaseUser mFirebaseUser;
        //private String mUsername;
        //private String mPhotoUrl;
    
    
        public chatFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.fragment_chat, container, false);
            webView_chat = (WebView) v.findViewById(R.id.webview_chat);
            eLinearLayout_chat = (LinearLayout) v.findViewById(R.id.LinearWebView_chat);
            mProgressBar_chat = (ProgressBar) v.findViewById(R.id.progressBar_chat);
            nav = (BottomNavigationView) v.getRootView().findViewById(R.id.bottom_navigation);
            //swipeRefreshLayout_chat = (SwipeRefreshLayout) v.findViewById(R.id.swipeweb_chat);
            webView_chat.loadUrl("https://drriyadh-lab.xyz/track/");
            webView_chat.getSettings().setJavaScriptEnabled(true);
            webView_chat.getSettings().setUseWideViewPort(true);
            webView_chat.getSettings().setDomStorageEnabled(true);
            webView_chat.getSettings().setSupportZoom(false);
            webView_chat.getSettings().setAppCacheEnabled(true);
            webView_chat.getSettings().setDatabaseEnabled(true);
            webView_chat.getSettings().setAllowFileAccess(true);
            webView_chat.getSettings().setAllowContentAccess(true);
            webView_chat.getSettings().setSupportMultipleWindows(true);
            webView_chat.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webView_chat.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            webView_chat.getSettings().setLoadWithOverviewMode(true);
    
    
            webView_chat.setWebViewClient(new WebViewClient() {
    
    
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    //mProgressBar_chat.setVisibility(View.VISIBLE);
    
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    mProgressBar_chat.setVisibility(View.GONE);
    
                }
    
                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    super.onReceivedError(view, request, error);
                    eLinearLayout_chat.setVisibility(View.VISIBLE);
                    view.setVisibility(View.GONE);
                }
    
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.contains("https://www.tidio.com/powered-by-tidio/live-chat/?platform=others&project=dc71bf5k0ncictpdhdyhujy2hzf9zn7x&device=mobile&utm_source=plugin_ref&utm_medium=widget_v4&utm_campaign=plugin_ref&utm_referrer=drriyadh-lab.xyz")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                    if (url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                    // all the rest open in Webview // make it false to fisable button of powered by tidio (still but not work in webview) make it to false to make whatsapp link opened , make it true to disbale whatsapp link and enable powered by tidio link work but in external browser
                    return false;
                }
    
            });
    
            return v;
        }
    
    }

我尝试了很多想法,但我没有找到任何解决方案,底部导航在MainActivity中,底部导航栏中有3个片段。其中一个片段是 WebView(如上所示)。当我输入键盘时,底部导航会向上移动键盘。我想在我输入键盘时隐藏它。

标签: androidandroid-fragmentskeyboardandroid-webview

解决方案


我已经解决了第一个问题, View v = inflater.inflate(R.layout.fragment_track, container, false); 像这样使它成为最终的

final View v = inflater.inflate(R.layout.fragment_track, container, false);

(将布局名称替换为您的布局名称)在此之后添加此

BottomNavigationView nav;

到你的片段类

之后添加

nav = (BottomNavigationView) getActivity().findViewById(R.id.bottom_navigation);

to your `public class (fragment name) extends Fragment`


 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
}

将 id 替换为 activity_main 中的底部导航 id

毕竟添加这个

v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                v.getWindowVisibleDisplayFrame(r);
                int orgnal = Resources.getSystem().getDisplayMetrics().heightPixels;
                int heightDiff = orgnal - v.getHeight();
                if (heightDiff > 500) { // if more than 100 pixels, its probably a keyboard...
                    nav.setVisibility(View.GONE);
                } else {

                    nav.setVisibility(View.VISIBLE);
                }
            }
        });

final View v = inflater.inflate(R.layout.your layout name, container, false);

和之前return v;

它应该可以工作,谢谢大家


推荐阅读