android 键盘布局缺少,Android 解决输入法键盘遮盖布局问题,程序员不得不用的方案,快捷!...

/**

* @param root 最外层布局,需要调整的布局

* @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部

*/

private void listenKeyboardLayout(final LinearLayout root, final View scrollToView) {

SoftKeyboardStateHelper keyboardStateHelper = new SoftKeyboardStateHelper(root);

keyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateListener() {

@Override

public void onSoftKeyboardOpened(int keyboardHeightInPx) {

Rect rect = new Rect();

// 获取root在窗体的可视区域

root.getWindowVisibleDisplayFrame(rect);

int[] location = new int[2];

// 获取scrollToView在窗体的坐标

scrollToView.getLocationInWindow(location);

// 计算root滚动高度,使scrollToView在可见区域的底部

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

root.scrollTo(0, srollHeight);

}

@Override

public void onSoftKeyboardClosed() {

// 键盘隐藏

root.scrollTo(0, 0);

}

});

}

在此要获取SoftKeyboardStateHelper类

效果图如下:

3df6fc2e18f32e2b12071e64fbd4201f.png

f98f878c2d0775707fc90cfc95135113.png

下面提供完整的代码及布局文件:

MainActivity

public class MainActivity extends Activity {

private LinearLayout mRoot;

private Button mSubmit;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mRoot = (LinearLayout) findViewById(R.id.root);

mSubmit = (Button) findViewById(R.id.submit);

listenKeyboardLayout(root, mSubmit);

}

/**

* @param root 最外层布局,需要调整的布局

* @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底

*/

private void listenKeyboardLayout(final LinearLayout root, final View scrollToView) {

SoftKeyboardStateHelper keyboardStateHelper = new SoftKeyboardStateHelper(root);

keyboardStateHelper.addSoftKeyboardStateListener(new SoftKeyboardStateListener() {

@Override

public void onSoftKeyboardOpened(int keyboardHeightInPx) {

Rect rect = new Rect();

// 获取root在窗体的可视区域

root.getWindowVisibleDisplayFrame(rect);

int[] location = new int[2];

// 获取scrollToView在窗体的坐标

scrollToView.getLocationInWindow(location);

// 计算root滚动高度,使scrollToView在可见区域的底部

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

root.scrollTo(0, srollHeight);

}

@Override

public void onSoftKeyboardClosed() {

// 键盘隐藏

root.scrollTo(0, 0);

}

});

}

activity_main.xml

android:id="@+id/root"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

android:gravity="center_vertical" >

android:layout_height="50dip"

android:hint="edit1"/>

android:layout_height="50dip"

android:hint="edit2"/>

android:layout_height="50dip"

android:hint="edit3"/>

android:layout_width="fill_parent"

android:layout_height="50dip"

android:text="submit"/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值