zoukankan      html  css  js  c++  java
  • android 聊天界面 软键盘弹出顶起界面解决

    参考:https://blog.csdn.net/I_do_can/article/details/71159198?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

    首先上布局

    <include
        android:id="@+id/include"
        layout="@layout/feedbacktitle"></include>
    
    <RelativeLayout
        android:layout_below="@id/include"
        android:id="@+id/move_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:layout_alignParentTop="true"
            android:layout_above="@id/rl_input"
            android:id="@+id/srl_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:srlEnableAutoLoadMore="true">
    
            <com.scwang.smartrefresh.layout.header.ClassicsHeader
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srlTextPulling="@string/down"
                app:srlTextRelease="@string/Release" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/include"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp" />
    
        </com.scwang.smartrefresh.layout.SmartRefreshLayout>
    
        <RelativeLayout
            android:layout_alignParentBottom="true"
            android:id="@+id/rl_input"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:paddingBottom="12dp">
    
            <TextView
                android:id="@+id/tv_send"
                android:layout_width="50dp"
                android:layout_height="35dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="25dp"
                android:background="@drawable/send_bg"
                android:gravity="center"
                android:text="发送"
                android:textColor="#ffffffff"
                android:textSize="13sp" />
    
            <ImageView
                android:id="@+id/iv_photo"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_alignTop="@id/tv_send"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="19dp"
                android:src="@mipmap/photo" />
    
            <EditText
                android:id="@+id/et_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:layout_toLeftOf="@id/tv_send"
                android:layout_toRightOf="@+id/iv_photo"
                android:background="@drawable/et_bg"
                android:hint="点击输入回复内容"
                android:paddingLeft="5dp"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:textColor="#ffa5a5a5"
                android:textSize="13sp" />
        </RelativeLayout>
    </RelativeLayout>
    
    java代码

    View move_root;
    View rl_input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // full screen
    

    //全屏
    this.getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

        move_root= findViewById(R.id.move_root);
        rl_input= findViewById(R.id.rl_input);
    
        addSoftKeyBoardListener(move_root,rl_input);
    
    }
    
    private ViewTreeObserver.OnGlobalLayoutListener mSoftKeyBoardListener;
    
    private class SoftKeyBoardListener implements ViewTreeObserver.OnGlobalLayoutListener {
    
        private View root;
        private View view;
    
        int lastHeight = 0;
        int lastBottom = -1;
    
        SoftKeyBoardListener(View r,View v) {
            root = r;
            view = v;
        }
    
        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            root.getWindowVisibleDisplayFrame(rect);
            if (lastBottom == -1) {
                lastBottom = rect.bottom;
                return;
            }
    
            int nb = rect.bottom;
            int ob = lastBottom;
    
            if (nb < ob) {
                // 键盘显示了, 滑上去
                int[] location = new int[2];
                view.getLocationInWindow(location);
                int scrollHeight = (location[1] + view.getHeight()) - nb;
                root.scrollTo(0, scrollHeight);
                lastHeight = scrollHeight;
            }
            else if (nb > ob) {
                // 键盘隐藏了, 滑下来
                root.scrollTo(0, 0);
            }
    
            if (nb != ob) {
                lastBottom = nb;
            }
        }
    }
    
    public void removeSoftKeyBoardListener(@NonNull View root) {
        if(mSoftKeyBoardListener != null){
            root.getViewTreeObserver().removeOnGlobalLayoutListener(mSoftKeyBoardListener);
        }
    }
    
    public void addSoftKeyBoardListener(final View root, final View view) {
        mSoftKeyBoardListener = new SoftKeyBoardListener(root,view);
        root.getViewTreeObserver().addOnGlobalLayoutListener(mSoftKeyBoardListener);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeSoftKeyBoardListener(move_root);
    }
  • 相关阅读:
    使用正则匹配数字
    钻石和玻璃球游戏(钻石位置不固定)
    简单绘图
    未解决问题02
    Sqlite3 实现学生信息增删改查
    【Python】科赫雪花绘制
    【Python爬虫】抖音去水印
    【MATLAB】数学计算软件 MathWorks MATLAB R2020a 中文破解版
    【C语言】用指针作为形参完成数据的升序排列
    【C语言】数组名作函数参数完成数据的升序排列
  • 原文地址:https://www.cnblogs.com/wang-jingyuan/p/13042379.html
Copyright © 2011-2022 走看看