ScrollView 与 键盘显示
程序员文章站
2022-06-25 09:39:49
<androidx.core.widget.NestedScrollView android:id="@+id/nsv_event" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_marginTop="15dp" android:orientation="vertical" android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/background"/> </LinearLayout> <LinearLayout android:layout_marginTop="15dp" android:orientation="vertical" android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> <LinearLayout android:layout_marginTop="15dp" android:orientation="vertical" android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="@dimen/body_layout_marginright" android:layout_marginLeft="@dimen/body_layout_marginleft" android:layout_marginTop="5dp" android:overScrollMode="never" android:layout_marginBottom="50dp"/> </LinearLayout> <View android:id="@+id/view_bottom" android:layout_width="match_parent" android:layout_height="120dp" android:visibility="gone"/> </LinearLayout> </androidx.core.widget.NestedScrollView>
@BindView(R.id.nsv_event) NestedScrollView nsv_event; @BindView(R.id.view_bottom) View mViewBottom;
SoftKeyBoardListener.setListener(EventAddActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() { @Override public void keyBoardShow(int height) { // Toast.makeText(EventAddActivity.this, "键盘显示 高度" + height, Toast.LENGTH_SHORT).show(); ViewGroup.LayoutParams layoutParams = mViewBottom.getLayoutParams(); layoutParams.height = height; mViewBottom.setLayoutParams(layoutParams); mViewBottom.setVisibility(View.VISIBLE); } @Override public void keyBoardHide(int height) { //Toast.makeText(EventAddActivity.this, "键盘隐藏 高度" + height, Toast.LENGTH_SHORT).show(); ViewGroup.LayoutParams layoutParams = mViewBottom.getLayoutParams(); layoutParams.height = 0; mViewBottom.setLayoutParams(layoutParams); mViewBottom.setVisibility(View.VISIBLE); } }); nsv_event.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener(){ public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { if (scrollY == 0) { //滑动隐藏键盘 hindInput(); } } });
显示键盘 public void showInput(final EditText et){ et.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.showSoftInput(et,InputMethodManager.SHOW_IMPLICIT); } 隐藏键盘 public void hindInput(){ InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); View v = getWindow().peekDecorView(); if (v != null) imm.hideSoftInputFromWindow(v.getWindowToken(),0); }
package com.xxx.View; import android.app.Activity; import android.graphics.Rect; import android.view.View; import android.view.ViewTreeObserver; import android.widget.Toast; public class SoftKeyBoardListener { private View rootView;//activity的根视图 int rootViewVisibleHeight;//纪录根视图的显示高度 private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener; public SoftKeyBoardListener(Activity activity) { //获取activity的根视图 rootView = activity.getWindow().getDecorView(); Rect r = new Rect(); rootView.getWindowVisibleDisplayFrame(r); rootViewVisibleHeight = r.height(); //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变 rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //获取当前根视图在屏幕上显示的大小 Rect r = new Rect(); rootView.getWindowVisibleDisplayFrame(r); int visibleHeight = r.height(); System.out.println(""+visibleHeight); if (rootViewVisibleHeight == 0) { rootViewVisibleHeight = visibleHeight; return; } //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变 if (rootViewVisibleHeight == visibleHeight) { return; } //根视图显示高度变小超过200,可以看作软键盘显示了 if (rootViewVisibleHeight - visibleHeight > 200) { if (onSoftKeyBoardChangeListener != null) { onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight); } rootViewVisibleHeight = visibleHeight; return; } //根视图显示高度变大超过200,可以看作软键盘隐藏了 if (visibleHeight - rootViewVisibleHeight > 200) { if (onSoftKeyBoardChangeListener != null) { onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight); } rootViewVisibleHeight = visibleHeight; return; } } }); } private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) { this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener; } public interface OnSoftKeyBoardChangeListener { void keyBoardShow(int height); void keyBoardHide(int height); } public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) { SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity); softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener); } }
本文地址:https://blog.csdn.net/qingchunqidewo/article/details/107441527
推荐阅读
-
macos big sur状态栏怎么显示键盘亮度?
-
用Spring Boot进行后端开发(二):与微信小程序的交互,在微信小程序端获取数据并显示
-
Android 显示和隐藏软键盘的方法(手动)
-
IOS TextFiled与TextView 键盘的收起以及处理键盘遮挡
-
iOS实现scrollview上拉显示Navbar下拉隐藏功能详解
-
Android Dialog中软键盘的显示与隐藏的示例
-
利用iOS手势与scrollView代理实现图片的放大缩小
-
Android软键盘状态弹出与消失的示例
-
iOS界面跳转时导航栏和tabBar的隐藏与显示功能
-
Android ScrollView嵌套ExpandableListView显示不正常的问题的解决办法