EditText初始不获得焦点及输入框被遮挡问题

介绍EditText和AutoCompleteTextView初始不获得焦点及解决软键盘弹出时遮挡输入框问题

1、activity启动时EditText不获得焦点
Activity启动时若有一个EditText默认,EditText获得焦点,去掉首次焦点,在manifest.xml中对应activity添加

1

android:windowSoftInputMode="stateHidden"

即可。

2、键盘弹出时输入框被压缩
输入框获得焦点弹出软键盘时,输入框被压缩,字体上浮,同时背景出现问题。如下:

EditText初始不获得焦点及输入框被遮挡问题

网上解决方法是添加

1

android:windowSoftInputMode="stateHidden|adjustResize"

1

2

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE

| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

经测试后无效。

解决方法为在外层布局中添加scrollView,因为scrollView只允许包含一个子View,所以如果出现问题布局已有外层layou,直接嵌套在ScrollView中即可,如下:

Java

1

2

3

4

5

6

7

8

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<!-- your layout -->

</ScrollView>

如果出现问题布局没有外层layout,还需要再嵌套一层RelativeLayout,如下:

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent" >

<!-- your layout -->

</RelativeLayout>

</ScrollView>