设置背景图片,软键盘弹出不将背景图片顶出屏幕

版权声明:https://mp.csdn.net/postedit

先上效果图

初始点击弹出软键盘

修改后

首先在 AndroidManifest.xml文件设置

这个的作用是让软键盘弹出时,不把布局给顶出去

解释:https://www.jianshu.com/p/dddcaac97cdc

效果:

思路: 自己计算软键盘的高度,可以用Rect或者PopupWindow 把下边的EditText 弹出来

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/meinv"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff00"
        android:text="这是测试软键盘用的"/>


    <LinearLayout
        android:id="@+id/ll"
        android:background="#88f1abcd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom ="true"
        android:layout_alignParentEnd="true"
        android:orientation="horizontal">


        <EditText
            android:id="@+id/edt_input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"/>

        <Button
            android:id="@+id/btnSend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="发送"/>
    </LinearLayout>

</RelativeLayout>

自定义软键盘高度获取:

package com.example.yangzhan.softkeyboard;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * created by ${yangzhan}
 * on 2019/4/28
 */
public class HideKeyboard extends PopupWindow implements ViewTreeObserver.OnGlobalLayoutListener {

    private Activity mActivity;
    private View mView;
    private int mHeightMax;
    private HeightListener mListener;

    public HideKeyboard(Activity activity){
        this.mActivity=activity;
        this.mView =new View(mActivity);

        setContentView(mView);

        //监听全局layout
        mView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        setBackgroundDrawable(new ColorDrawable(0));

        //设置宽高
        setWidth(0);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);

        //设置软键盘弹出方式
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    }

    public HideKeyboard init(){
        if (!isShowing()){
            final View view =mActivity.getWindow().getDecorView();
            view.post(new Runnable() {
                @Override
                public void run() {
                    showAtLocation(view, Gravity.NO_GRAVITY,0,0);
                }
            });
        }
        return this;
    }

    public HideKeyboard setHeightListener(HeightListener listener){
        this.mListener=listener;
        return this;
    }


    public interface HeightListener {
        void onHeightChanged(int height);
    }

    @Override
    public void onGlobalLayout() {

        Rect rect = new Rect();
        mView.getWindowVisibleDisplayFrame(rect);
        if (rect.bottom > mHeightMax) {
            mHeightMax = rect.bottom;
        }

        // 两者的差值就是键盘的高度
        int keyboardHeight = mHeightMax - rect.bottom;
        if (mListener != null) {
            mListener.onHeightChanged(keyboardHeight);
        }
    }
}

使用:

package com.example.yangzhan.softkeyboard;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "123";
    private LinearLayout mLinearLayout;

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

        mLinearLayout =findViewById(R.id.ll);

        new HideKeyboard(this).init().setHeightListener(new HideKeyboard.HeightListener() {
            @Override
            public void onHeightChanged(int height) {
                Log.e(TAG, "onHeightChanged: height ="+height);
                mLinearLayout.setTranslationY(-height);
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32425789/article/details/89638766
今日推荐