EditText弹出软键盘,上移底部区域

先看效果:

原布局:

EditText弹出软键盘,上移底部区域_第1张图片

 

未处理,弹出软键盘:软键盘遮住了图片

EditText弹出软键盘,上移底部区域_第2张图片

 

经过处理,再弹出软键盘:图片顶上去了

EditText弹出软键盘,上移底部区域_第3张图片

 

源码:

package com.paint.user.test0104;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

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

    private void initView() {
        LinearLayout fu_layout = (LinearLayout)findViewById(R.id.fu_layout);
        LinearLayout zi_layout = (LinearLayout)findViewById(R.id.zi_layout);
        View v_foot = findViewById(R.id.v_foot);

        addLayoutListener(fu_layout, zi_layout,v_foot);

    }

    /*
    * main: 父布局,最外层的LinearLayout
    * scroll:子布局:LinearLayout
    * foot : 最外层的view
    **/

    private void addLayoutListener(final View main, final View scroll, final View foot) {

        main.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            Rect rect = new Rect();
            main.getWindowVisibleDisplayFrame(rect);
            int nav_height = getNavigationBarHeight(MainActivity.this);
            int mainInvisibleHeight = main.getRootView().getHeight() - nav_height - (rect.bottom - rect.top);
            if (mainInvisibleHeight > 260) {
                if (foot.getVisibility() == View.GONE) {
                    int[] location = new int[2];
                    scroll.getLocationInWindow(location);
                    int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
                    ViewGroup.LayoutParams Params = foot.getLayoutParams();
                    Params.height = srollHeight;
                    foot.setLayoutParams(Params);
                    foot.setVisibility(View.VISIBLE);

                }
            } else {
                foot.setVisibility(View.GONE);
            }
        });
    }

    /**
     * 获取虚拟键的高度
     * @param context
     * @return
     */
    public static int getNavigationBarHeight(Context context) {
        int result = 0;
        if (hasNavBar(context)) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    /**
     * 判断是否有虚拟键
     * @param context
     * @return
     */
    public static boolean hasNavBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {
        }
        return hasNavigationBar;
    }


}

再看下,布局文件:




        

        


            

            

        

        

    

注意!!!,第一个View中,高度一定要自适应,如果写死,则软键盘处理会失效!

注意!!!,第一个View中,高度一定要自适应,如果写死,则软键盘处理会失效!

注意!!!,第一个View中,高度一定要自适应,如果写死,则软键盘处理会失效!

 

你可能感兴趣的:(拒绝重复造轮子,软键盘处理,软键盘打开上移布局)