Android 软键盘覆盖并抬起输入框

页面有EditText,获取焦点时,默认情况下,软键盘会把整个页面布局给顶上去

假设我们想要软键盘覆盖在原页面上,只是把EditText抬起到软键盘之上,

或是想监测软键盘显示或隐藏时,进行一些操作

效果图

Android 软键盘覆盖并抬起输入框_第1张图片    Android 软键盘覆盖并抬起输入框_第2张图片


思路是用屏幕高度减键盘抬起时,页面可视区域的高度,得到软键盘的高度

关键代码:

Rect r = new Rect();
((Activity)(mContext)).getWindow().getDecorView().getWindowVisibleDisplayFrame(r);

Demo代码如下

布局文件




        

        

        

        

            

            


package com.example.softkeyboardtest;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText mEtInput;
    Button mBtnSend;
    TextView mTvShow;
    View mDivider;
    LinearLayout mLlInputPanel;

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

        mEtInput = (EditText)findViewById(R.id.et_input);
        mBtnSend = (Button)findViewById(R.id.btn_send);
        mTvShow = (TextView)findViewById(R.id.tv_show);
        mDivider = findViewById(R.id.v_divider);
        mLlInputPanel = (LinearLayout)findViewById(R.id.ll_input);

        mBtnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mEtInput.getText().toString())) {
                    mTvShow.setText(mEtInput.getText().toString());
                    mEtInput.setText("");
                }
            }
        });

        ((SoftKeyboardSizeWatchLayout)findViewById(R.id.keyboard_layout)).addResizeListener(new SoftKeyboardSizeWatchLayout.OnResizeListener() {
            @Override
            public void OnSoftPop(int height) {
                mLlInputPanel.setTranslationY(-height);
                mDivider.setTranslationY(-height);
            }

            @Override
            public void OnSoftClose() {
                mLlInputPanel.setTranslationY(0);
                mDivider.setTranslationY(0);
            }
        });
    }

    public void showKeyboard() {
        final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        //EditText未绘制完成时,抬起软键盘会失败。所以延时执行
        new Handler().postDelayed(new Runnable(){
            public void run() {
                if(imm != null){
                    if(! imm.showSoftInput(mEtInput, 0)){
                        imm.toggleSoftInput(0,0);
                    }
                }
            }
        }, 100);
    }

    public void hideKeyboard() {
        mEtInput.clearFocus();
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm != null){
            imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (isShouldHideInput(mEtInput, ev)) {
                hideKeyboard();
            }
            return super.dispatchTouchEvent(ev);
        }

        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /**
     * 判断点击event 坐标是否位于View v之内
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();

            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                return false;
            } else {
                v.setFocusable(false);
                v.setFocusableInTouchMode(true);
                return true;
            }
        }
        return false;
    }
}

package com.example.softkeyboardtest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19.
 */

public class SoftKeyboardSizeWatchLayout extends RelativeLayout {
    private Context mContext;
    private boolean mIsSoftKeyboardPop = false;
    private int mScreenHeight = 0;
    private int mOldh = -1;
    private int mNowh = -1;

    public SoftKeyboardSizeWatchLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){

            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                ((Activity)(mContext)).getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                if(mScreenHeight == 0){
                    mScreenHeight = r.bottom;
                }
                mNowh = mScreenHeight - r.bottom;
                if(mOldh != -1 && mNowh != mOldh) {
                    if (mNowh > 0) {
                        mIsSoftKeyboardPop = true;
                        if (mListenerList != null) {
                            for (OnResizeListener l : mListenerList) {
                                l.OnSoftPop(mNowh);
                            }
                        }
                    } else {
                        mIsSoftKeyboardPop = false;
                        if (mListenerList != null) {
                            for (OnResizeListener l : mListenerList) {
                                l.OnSoftClose();
                            }
                        }
                    }
                }
                mOldh = mNowh;
            }
        });

    }

    public boolean ismIsSoftKeyboardPop() {
        return mIsSoftKeyboardPop;
    }

    public List mListenerList;
    public void addResizeListener(OnResizeListener listener){
        if(null == mListenerList){
            mListenerList = new ArrayList();
        }
        mListenerList.add(listener);
    }


    public interface OnResizeListener {
        /**
         * 软键盘弹起
         */
        void OnSoftPop(int height);

        /**
         * 软键盘关闭
         */
        void OnSoftClose();
    }

}


你可能感兴趣的:(Android)