解决 android 输入法弹出输入框被遮挡的问题
由于本人懒,也从不写博客,之所以写是因为这次开发 ’又遇到‘ 输入法这个问题了,好久以前有遇到过,突然发现还是很有必要把他分享出来,言归正传,下面就把解决的几种方法贴上。
1,首先看效果图:
2,实现效果图 (输入框没被遮住):
解决方法一:
在 AndroidManifest.xml 文件中的activity里加入 android:windowSoftInputMode=“stateVisible|adjustResize”
解决方法二:
在 Activity 的 onCreate 里加入 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
public class TestActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
解决方法三:
把你的 Layout 外面再加一层 ScrollView , 这样软键盘和输入框就可以一起滚动
解决方法四:
1,Activity 代码
public class TestActivity extends FragmentActivity {
// 最外层需要调整的Layout
private RelativeLayout rootView;
// 滚动的Layout
private LinearLayout scrollToInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
rootView = (RelativeLayout) findViewById(R.id.rootView);
scrollToInput = (LinearLayout) findViewById(R.id.scrollToInput);
rootView.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//取得 rootView 可视区域
rootView.getWindowVisibleDisplayFrame(rect);
//取得 rootView 不可视区域高度 (被其他View遮挡的区域高度)
int rootInvisibleHeight = rootView.getRootView().getHeight() - rect.bottom;
//要是不可视区域高度大于100,则输入键盘就显示
if (rootInvisibleHeight > 100) {
int[] location = new int[2];
//取得 scrollToInput 的坐标
scrollToInput.getLocationInWindow(location);
//计算滚动高度(rootView),这样 (scrollToInput)在可视区域
int srollHeight = (location[1] + scrollToInput.getHeight()) - rect.bottom;
rootView.scrollTo(0, srollHeight);
} else {
//隐藏软键盘
rootView.scrollTo(0, 0);
}
}
});
}
}
2,activity_test.xml
<LinearLayout
android:id="@+id/scrollToInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#B2AFAF"
android:orientation="horizontal" >
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/et_live_bg"
android:gravity="left|center"
android:hint="听说在这里评论的人超帅..."
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>