解决 Android 软键盘挡住输入框的问题
当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入。
下面提供三种解决办法:
**方法一:**在你的activity中的oncreate中setContentView之前写上这个代码getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
**方法二:**在项目的AndroidManifest.xml文件中界面对应的里加入android:windowSoftInputMode="stateVisible|adjustResize",这样会让屏幕整体上移。如果加上的是 android:windowSoftInputMode="adjustPan"这样键盘就会覆盖屏幕。
**方法三:**把顶级的layout替换成ScrollView,或者说在顶级的Layout上面再加一层ScrollView的封装。这样就会把软键盘和输入框一起滚动了,软键盘会一直处于底部。
在我们的LinearLayout布局外添加ScrollView
方法三示例:
由原来的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout\_width="fill\_parent"
android:layout\_height="fill\_parent">
......
</LinearLayout>
改为:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout\_width="fill\_parent"
android:layout\_height="fill\_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout\_width="fill\_parent"
android:layout\_height="fill\_parent">
......
</LinearLayout>
</ScrollView>