如何在 webview 中输入键盘时隐藏底部导航栏
How to hide bottom navagation bar when type in keyboard in 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(如上所示)。当我输入键盘时,底部导航会在键盘上移动。我想在键盘输入时隐藏它。
我已经解决了问题
首先制作 View v = inflater.inflate(R.layout.fragment_track, container, false);
让它成为最终的
像这样
final View v = inflater.inflate(R.layout.fragment_track, container, false);
(将布局名称替换为您的布局名称)在此之后
添加这个
BottomNavigationView nav;
给你的
片段 class
之后添加
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;
之前
应该可以
谢谢大家
如何在其中一个片段的 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(如上所示)。当我输入键盘时,底部导航会在键盘上移动。我想在键盘输入时隐藏它。
我已经解决了问题
首先制作 View v = inflater.inflate(R.layout.fragment_track, container, false);
让它成为最终的
像这样
final View v = inflater.inflate(R.layout.fragment_track, container, false);
(将布局名称替换为您的布局名称)在此之后 添加这个
BottomNavigationView nav;
给你的 片段 class
之后添加
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;
应该可以 谢谢大家