100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android显示输入法键盘布局 android 解决输入法键盘遮盖布局问题

android显示输入法键盘布局 android 解决输入法键盘遮盖布局问题

时间:2023-05-15 07:01:48

相关推荐

android显示输入法键盘布局 android 解决输入法键盘遮盖布局问题

这里采用滚动布局来解决输入法遮盖布局的问题,方法如下:

/**

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

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

*/

private void controlKeyboardLayout(final View root, final View scrollToView) {

root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

Rect rect = new Rect();

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

root.getWindowVisibleDisplayFrame(rect);

//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)

int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;

//若不可视区域高度大于100,则键盘显示

if (rootInvisibleHeight > 100) {

int[] location = new int[2];

//获取scrollToView在窗体的坐标

scrollToView.getLocationInWindow(location);

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

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

root.scrollTo(0, srollHeight);

} else {

//键盘隐藏

root.scrollTo(0, 0);

}

}

});

}

效果图如下:

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

1. 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);

controlKeyboardLayout(mRoot, mSubmit);

}

/**

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

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

*/

private void controlKeyboardLayout(final View root, final View scrollToView) {

root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

Rect rect = new Rect();

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

root.getWindowVisibleDisplayFrame(rect);

//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)

int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;

//若不可视区域高度大于100,则键盘显示

if (rootInvisibleHeight > 100) {

int[] location = new int[2];

//获取scrollToView在窗体的坐标

scrollToView.getLocationInWindow(location);

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

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

root.scrollTo(0, srollHeight);

} else {

//键盘隐藏

root.scrollTo(0, 0);

}

}

});

}

}

2. 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"/>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。