07/31
2013

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

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

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

android:windowSoftInputMode="stateHidden"

即可。

 

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

网上解决方法是添加

android:windowSoftInputMode="stateHidden|adjustResize"

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                             | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

经测试后无效。

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

<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,如下:

<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>

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

  1. 看过楼主的一些文章表示很喜欢,会持续关注,有一个问题想请教,在本地加载网页的时候也有可能把输入框给挤掉,不知道楼主碰到过没有