android 点击弹出键盘,Android 点击输入框弹出键盘,布局随键盘往上移动

博客介绍了Android点击弹出键盘时的布局处理。展示了正常登陆页面及布局随键盘上移、登陆按钮不被覆盖的效果。给出代码,通过判断窗体不可视区域高度来处理键盘显示与隐藏时布局滚动,还说明了在Activity的onCreate()中调用该方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、效果图:

792fc432db46?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.正常登陆页面

792fc432db46?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

2.布局随键盘往上移动的布局,登陆按钮不会被覆盖

二、Code:

/**

*  1、获取main在窗体的可视区域

*  2、获取main在窗体的不可视区域高度

*  3、判断不可视区域高度,之前根据经验值,在有些手机上有点不大准,现改成屏幕整体高度的1/3

*      1、大于屏幕整体高度的1/3:键盘显示  获取Scroll的窗体坐标

*                          算出main需要滚动的高度,使scroll显示。

*      2、小于屏幕整体高度的1/3:键盘隐藏

*

* @param main 根布局

* @param scroll 需要显示的最下方View

*/

public static void addLayoutListener(final View main, final View scroll) {

main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

Rect rect = new Rect();

main.getWindowVisibleDisplayFrame(rect);

int screenHeight = main.getRootView().getHeight();

int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;

if (mainInvisibleHeight > screenHeight / 4) {

int[] location = new int[2];

scroll.getLocationInWindow(location);

int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;

main.scrollTo(0, srollHeight);

} else {

main.scrollTo(0, 0);

}

}

});

}

三、调用方式:

在Activity的onCreate()中调用,

792fc432db46?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

一句话搞定

仅此记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值