app弹出输入法软件的时候,遮挡住输入框问题的解决方案
2018-01-02 11:39
381 查看
前段时维护以前的项目,出现了一个奇怪的问题——软键盘弹出后挡住了输入框,当时在AndroidManifest.xml中对应的Activity配置了:
但是没有效果,甚至一部分机型出现了背景图被严重压缩导致变形失真的情况。
然后尝试了在对应的Activity的onCretae()的setContentView()方法前面添加:
效果依然是差强人意,后来甚至采取了在ScrollView里面布局背景View的思路,这样虽然解决了输入框被遮挡的问题,但是每次用户输入的时候,都要去滑动一下背景View,总感觉体验不佳,违背操作习惯;经过查阅资料和参考网络前辈们的思路,有了以下解决方案:
xml布局:
Activity核心代码:
运行效果如下:
软键盘还未弹出时的效果:
软键盘弹出后的效果:
该方法通过了多机型的适配,无异常信息出现。
新增gif动画效果:
android:windowSoftInputMode="stateVisible|adjustResize" android:configChanges="keyboardHidden|orientation|screenSize"
但是没有效果,甚至一部分机型出现了背景图被严重压缩导致变形失真的情况。
然后尝试了在对应的Activity的onCretae()的setContentView()方法前面添加:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
效果依然是差强人意,后来甚至采取了在ScrollView里面布局背景View的思路,这样虽然解决了输入框被遮挡的问题,但是每次用户输入的时候,都要去滑动一下背景View,总感觉体验不佳,违背操作习惯;经过查阅资料和参考网络前辈们的思路,有了以下解决方案:
xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llLoginView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FAFAFA" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="300dp" android:background="#8fE095" android:scaleType="centerInside" android:src="@drawable/logo" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="5dp" android:paddingRight="15dp" android:paddingLeft="15dp"> <EditText android:id="@+id/etuUerName" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:hint="请输入用户名" android:inputType="text" android:maxLength="18" android:singleLine="true" android:textColor="#808080" android:background="@null" android:textSize="18sp" /> <EditText android:id="@+id/etUserPwd" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:hint="请输入密码" android:inputType="numberPassword" android:maxLength="20" android:singleLine="true" android:textColor="#808080" android:background="@null" android:textSize="18sp" /> <CheckBox android:id="@+id/cbLoginCheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住用户名" android:textColor="#808080" android:textSize="15sp" android:layout_marginLeft="15dp"/> <Button android:id="@+id/btLogin" android:layout_width="300dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:background="@drawable/login_buton" android:gravity="center" android:text="立 即 登 录" android:textSize="18sp" android:textColor="#ffffff" android:textStyle="bold" android:fontFamily="微软雅黑"/> </LinearLayout> </LinearLayout>
Activity核心代码:
public class SoftInputModeActivity extends AppCompatActivity { private Button btLogin; private LinearLayout llLoginView; private int scrollToPosition = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getSupportActionBar().hide(); setContentView(R.layout.activity_soft_input_mode); btLogin = (Button) findViewById(R.id.btLogin); llLoginView = (LinearLayout) findViewById(R.id.llLoginView ); autoScrollView(); } private void autoScrollView() { llLoginView.getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { computePosition(); } }); } private void computePosition(){ Rect rect = new Rect(); //获取root在窗体的可视区域 llLoginView.getWindowVisibleDisplayFrame(rect); //获取root在窗体的不可视区域高度(被遮挡的高度) int rootInvisibleHeight = llLoginView.getRootView().getHeight() - rect.bottom; //若不可视区域高度大于150,则键盘显示 if (rootInvisibleHeight > 150) { //获取scrollToView在窗体的坐标,location[0]为x坐标,location[1]为y坐标 int[] location = new int[2]; btLogin.getLocationInWindow(location); //计算root滚动高度,使scrollToView在可见区域的底部 int scrollHeight = (location[1] + btLogin.getHeight()) - rect.bottom; //注意:scrollHeight是相对移动距离,而scrollToPosition是绝对移动距离 scrollToPosition += scrollHeight; } else {//否则键盘隐藏 scrollToPosition = 0; } llLoginView.scrollTo(0, scrollToPosition); } }
运行效果如下:
软键盘还未弹出时的效果:
软键盘弹出后的效果:
该方法通过了多机型的适配,无异常信息出现。
新增gif动画效果:
相关文章推荐
- 安卓学习笔记---解决在在Edittext输入的时候,输入框被软键盘遮挡部分内容的问题(一)
- 安卓学习笔记---解决在在Edittext输入的时候,输入框被软键盘遮挡部分内容的问题(二)
- 解决popupwindow中有输入框的时候,弹出软键盘遮挡布局的问题
- 输入的时候被键盘遮挡的问题
- swift3.0键盘弹起遮挡输入框问题的解决方案
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
- 解决webview中输入框完成输入隐藏键盘后滑动界面又弹出软键盘问题
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
- Android弹出软键盘遮挡输入框问题.
- 关于手机端IOS系统微信中虚拟键盘遮挡input输入框问题的解决方案
- AppCompatActivity 去掉标题栏和EditText弹出软键盘遮住输入框问题
- 封装有输入框的 PopupWindow,并解决输入法弹出将其遮挡问题
- 解决设置状态栏透明后,软键盘弹出遮挡输入框的问题
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
- Android程序中输入法弹出的时候如何不遮挡输入,与布局相配
- iOS键盘弹出遮挡输入框问题
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
- input输入框输入中文时,需要计数input输入的汉字数,解决拼音输入的时候计数问题
- Android 全屏情况下 webview中输入框被软件盘遮挡问题解决方案