android软键盘弹出引起的各种不适终极解决方案

android软键盘弹出引起的各种不适终极解决方案

以下描述如何解决ListView高度小于0时出现的UI问题。

创建RelativeLayout的子类TxrjRelativeLayout

public class TxrjRelativeLayout extends RelativeLayout {
    private int count = 0;

    public TxrjRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("txrjsms " + count++, "=>onSizeChanged called! w=" + w
                + ",h=" + h + ",oldw=" + oldw + ",oldh=" + oldh);
    }
}

使用TxrjRelativeLayout 提供sms_message_list.xml文件中的根节点RelativeLayout。

运行程序查看日志:

07-20 12:56:54.774: E/txrjsms 0(23195): =>TxrjRelativeLayout.onSizeChanged called! w=480,h=762,oldw=0,oldh=0
07-20 12:56:54.784: E/txrjsms 0(23195): =>TxrjListView.onSizeChanged called! w=480,h=605,oldw=0,oldh=0
07-20 12:56:55.024: E/txrjsms 1(23195): =>TxrjListView.onSizeChanged called! w=480,h=373,oldw=480,oldh=605
07-20 12:57:08.377: E/txrjsms 1(23195): =>TxrjRelativeLayout.onSizeChanged called! w=480,h=353,oldw=480,oldh=762 // 762-353=409
07-20 12:57:08.377: E/txrjsms 2(23195): =>TxrjListView.onSizeChanged called! w=480,h=-36,oldw=480,oldh=373 // 373-(-36)=409

07-20 12:57:48.396: E/txrjsms 3(23195): =>TxrjListView.onSizeChanged called! w=480,h=-37,oldw=480,oldh=-36
07-20 12:57:57.164: E/txrjsms 4(23195): =>TxrjListView.onSizeChanged called! w=480,h=-9,oldw=480,oldh=-37
07-20 12:58:00.338: E/txrjsms 5(23195): =>TxrjListView.onSizeChanged called! w=480,h=19,oldw=480,oldh=-9
07-20 12:58:06.864: E/txrjsms 6(23195): =>TxrjListView.onSizeChanged called! w=480,h=47,oldw=480,oldh=19
07-20 12:58:07.745: E/txrjsms 7(23195): =>TxrjListView.onSizeChanged called! w=480,h=75,oldw=480,oldh=47
07-20 12:58:08.776: E/txrjsms 8(23195): =>TxrjListView.onSizeChanged called! w=480,h=103,oldw=480,oldh=75
07-20 12:58:09.647: E/txrjsms 9(23195): =>TxrjListView.onSizeChanged called! w=480,h=131,oldw=480,oldh=103
07-20 12:58:10.477: E/txrjsms 10(23195): =>TxrjListView.onSizeChanged called! w=480,h=159,oldw=480,oldh=131
07-20 12:58:11.378: E/txrjsms 11(23195): =>TxrjListView.onSizeChanged called! w=480,h=187,oldw=480,oldh=159
07-20 12:58:12.299: E/txrjsms 12(23195): =>TxrjListView.onSizeChanged called! w=480,h=196,oldw=480,oldh=187
07-20 12:58:15.813: E/txrjsms 2(23195): =>TxrjRelativeLayout.onSizeChanged called! w=480,h=419,oldw=480,oldh=353 // 419-353=66
07-20 12:58:15.813: E/txrjsms 13(23195): =>TxrjListView.onSizeChanged called! w=480,h=262,oldw=480,oldh=196
07-20 12:58:15.913: E/txrjsms 3(23195): =>TxrjRelativeLayout.onSizeChanged called! w=480,h=762,oldw=480,oldh=419 // 762-419=343
07-20 12:58:15.923: E/txrjsms 14(23195): =>TxrjListView.onSizeChanged called! w=480,h=605,oldw=480,oldh=262

通过计算,可以得出软键盘的高度是409,其中候选区高度是66,键盘区高度是343。

实现TxrjListView扩展ListView,在onSizeChanged()方法中根据当前高度判断是否将ListView设定为不可见。

如果高度小于等于0,那么将ListView设定为不可见,然后就不会出现文章adjustResize和adjustPan的比较中提到的UI重叠BUG。

public class TxrjListView extends ListView {
    public int count = 0;

    public TxrjListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("txrjsms " + count++, "=>TxrjListView.onSizeChanged called! w=" + w
                + ",h=" + h + ",oldw=" + oldw + ",oldh=" + oldh);
        setVisibility(h <= 0 ? View.INVISIBLE : View.VISIBLE); // 高度小于等于0不可见,高度大于0可见。
    }
}

posted on 2013-07-20 12:04  勤修  阅读(7429)  评论(0编辑  收藏  举报

导航