Android判断输入框的状况

整体思路

定义一个layout,重写onMeasure。定义一个变量,记录两次方法的差值。通过这个差值来判断是否弹出输入框。当然,这是取巧。

1、定义一个布局

package com.example.testandroid.view;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.FrameLayout;


/**
 * Created by Administrator on 13-6-27.
 *
 * @author ll
 * @version 2.0.0
 */
public class MonitorSizeEventFrameLayout extends FrameLayout {

    private int mPreviousHeight;

    /**
     * MonitorSizeEventFrameLayout
     *
     * @param context context
     * @param attrs   attrs
     */
    public MonitorSizeEventFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (mPreviousHeight != 0) {
            int navigationBarHeight = getNavigationBarHeight();
            if (measureHeight < mPreviousHeight - navigationBarHeight) {    // 减去底部虚拟键盘高度,过滤掉虚拟键盘显隐产生的页面高度变化
                System.out.println("打开了");
            } else if (measureHeight - navigationBarHeight > mPreviousHeight) {     
                System.out.println("关闭了");
            }
        }
        mPreviousHeight = measureHeight;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 获取底部虚拟键Navigation Bar的高度
     * @return
     */
    private int getNavigationBarHeight() {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }
}

2、在Actiivty的布局文件中添加自定义布局

3、在manifest添加

android:windowSoftInputMode="stateHidden|adjustResize"

你可能感兴趣的:(Android判断输入框的状况)