监听键盘弹出与隐藏

布局监听
public class MyMeasureLayout extends RelativeLayout {
    public static final String TAG = "MyMeasureLayout";
    public static final boolean DEBUG = true;

    private FragmentSizeObserver mObserver;

    private int initBottom = -1;
    private int initLeft  = -1;

    public MyMeasureLayout(Context context){
        this(context, null);
    }

    public MyMeasureLayout(Context context, AttributeSet attrs){
        this(context, attrs, 0);
    }

    public MyMeasureLayout(Context context, AttributeSet attrs,
                                 int defStyleAttr){
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        super.onLayout(changed, l, t, r, b);
        LogUtils.i( " changed  "+changed +" b "+b);
        if(initBottom == -1 || initLeft == -1){
            initBottom = b;
            initLeft   = l;
            return;
        }
        if(changed){
            LogUtils.i(" height "+b +" width "+r
                    +" initBottom "+initBottom+" initLeft "+initLeft);
            int height = b - initBottom; //高度变化值(弹出输入法,布局变小,则为负值)
            int width  = r - initLeft;  // 当前屏幕宽度(对应输入法而言无影响)
            onInputSizeChanged(width, height);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
    private void onInputSizeChanged(int widthDiff,int heightDiff) {
        if(mObserver != null){
            mObserver.onFragmentSizeChanged(widthDiff, heightDiff);
        }
    }
    public void registFragmentSizeObserver(FragmentSizeObserver observer) {
        mObserver = observer;
    }
    public interface FragmentSizeObserver{
        public void onFragmentSizeChanged(int wdiff,int hdiff);
    }
}


activity 实现
rl.registFragmentSizeObserver(this);
//实现
public void onFragmentSizeChanged(int wdiff, int hdiff) {
    LogUtils.i("onFragmentSizeChanged wdiff " + wdiff + " hdiff "
                + hdiff);
    //      doFragmentSizeChanged(wdiff, hdiff);
    if(hdiff < -100){ // show soft input
            // 处理输入法弹出事件
        
    }
    else if(hdiff == 0){ // hide soft input
        // 处理输入法隐藏事件
        if(null != rl){
            
            LogUtils.i("--keybord hide soft input--add messageRootListener ");
        }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值