1、在Android系统中,有时候在文本框中输入文字后,操作按钮被输入法遮挡了,不得不关闭输入法才可以继续操作。默认时时这样的,很麻烦。
2、于是,查了一些资料,终于找到方法。
先来看下代码与效果:
<?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"
android:orientation="vertical"
android:background="#ffffffff" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="30dp" >
<Button
android:id="@+id/Date"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:singleLine="true"
android:layout_marginTop="3dp"/>
<Button
android:id="@+id/Time"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:singleLine="true"
android:layout_marginTop="3dp" />
</LinearLayout>
<EditText
android:id="@+id/mText"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="@drawable/shape"
android:textColor="#aa000000"
android:textSize="20dp"
android:gravity="top"
android:layout_margin="3dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/set"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Set"
android:textSize="20dp"
/>
<Button
android:id="@+id/discard"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Cancel"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
可以看出这已经实现了避免软键盘遮挡页面。
显示过程:
不需要更改局部文件,而是修改AndroidManifest.xml文件,在Activity属性中加一条:
<activity
android:name="com.ideal.studys.EditTime"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden|adjustResize" >
</activity>
需要注意的是:TextView中android:layout_height="100px",后面的大小要根据硬件平台的分辨率适当修改才会有效。这个还不是很清楚。
附加:关于android:windowSoftInputMode=“”的一些参数:
值 | 描述 |
"stateUnspecified" | 软键盘的状态(是否它是隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。 这个是为了软件盘行为默认的设置。 |
"stateUnchanged" | 软键盘被保持无论它上次是什么状态,是否可见或隐藏,当主窗口出现在前面时。 |
"stateHidden" | 当用户选择该Activity时,软键盘被隐藏——也就是,当用户确定导航到该Activity时,而不是返回到它由于离开另一个Activity。 |
"stateAlwaysHidden" | 软键盘总是被隐藏的,当该Activity主窗口获取焦点时。 |
"stateVisible" | 软键盘是可见的,当那个是正常合适的时(当用户导航到Activity主窗口时)。 |
"stateAlwaysVisible" | 当用户选择这个Activity时,软键盘是可见的——也就是,也就是,当用户确定导航到该Activity时,而不是返回到它由于离开另一个Activity。 |
"adjustUnspecified" | 它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区域中可见的。这个是主窗口默认的行为设置。 |
"adjustResize" | 该Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间 |
"adjustPan" | 该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作 |