起因:在有些Android电视系统上面在弹出输入法软键盘时会遮住输入框中输入的内容信息(由其输入框在屏幕底部显示)
目前解决的方案是:
第一步: 监听输入法软键盘弹起与收起的事件
第二步: 在监听的方法中获取界面根布局,设置布局的translationY值即可让界面整体向上或向下移动到指定位置
通过上面的方法,即可解决输入框被遮住的问题
mActivityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewGlobalLayoutListener());
以下是实现该功能的代码:
class ViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
// mActivityRootView为Activity布局文件的根布局对象
mActivityRootView.getWindowVisibleDisplayFrame(r);
//比较Activity根布局与当前布局的大小
int heightDiff = mActivityRootView.getRootView().getHeight() - (r.bottom - r.top);
float translationY = mActivityRootView.getTranslationY();
if (heightDiff > 100) {
//大小大于100时,为虚拟解盘弹起
if (translationY > -1.0f) {
// 移动位置(向上移动300px)
startAnimator(0.0f, -300);
}
} else {
//大小小于100时,为不显示虚拟键盘或虚拟键盘隐藏
if (translationY < -1.0f) {
// 还原位置(向下移动300px)
startAnimator(-300, 0.0f);
}
}
}
private void startAnimator(float fromTranslationY, float toTranslationY) {
// 带动画效果的向上或向下移动
ObjectAnimator animator = ObjectAnimator.ofFloat(
mActivityRootView, "translationY", fromTranslationY, toTranslationY);
animator.setDuration(200);
animator.start();
}
}