您的位置:首页 > 移动开发

app弹出输入法软件的时候,遮挡住输入框问题的解决方案

2018-01-02 11:39 381 查看
前段时维护以前的项目,出现了一个奇怪的问题——软键盘弹出后挡住了输入框,当时在AndroidManifest.xml中对应的Activity配置了:

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动画效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐