在Webview中键入键盘时如何隐藏底部导航栏

时间:2020-08-25 07:10:59

标签: android android-fragments keyboard android-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(如上所示)。当我输入键盘时,底部的导航键会向上移动键盘。我想在输入键盘时将其隐藏。

1 个答案:

答案 0 :(得分:0)

我已经解决了问题 首先制作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) {
}

在activity_main中用底部导航ID替换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;

之前

它应该工作 谢谢大家