当前位置: 移动技术网 > IT编程>开发语言>.net > ScrollView 与 键盘显示

ScrollView 与 键盘显示

2020年07月23日  | 移动技术网IT编程  | 我要评论
<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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网