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

android windowSoftInputMode说明

2015-08-15 15:53 495 查看
/**
* 作者:crazyandcoder
* 联系:
*      QQ   : 275137657
*      email: lijiwork@sina.com
* 转载请注明出处!
*/


 windowSoftInputMode说明

       当我们进入到一个包含可以输入的控件的页面时,有的时候需要我们自动的弹出输入框,这样做的好处就是方便用户输入,虽然是一个小小的设计,但是能方便用户使用,android的设计一个特点就是怎么方便用户怎么设计,这就涉及到用户体验的问题了。那么我们该如何设计这个需求呢?在工程的androidManifest.xml文件中,有个属性是:

android:windosSoftInputMode,通过设置这个,我们便可以控制软件的状态。

       这个属性的设置会影响着两件事,我们查看一下官方文档上面的介绍:

The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
软键盘的状态,当activity获得焦点时,它是隐藏还是显示。
The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.
activity的窗口的调整,是否减少activity主窗口的大小以便腾出空间来放置软键盘或者当前活动窗口的部分被软键盘覆盖时它的内容的当前焦点是可见的。

        这个属性的值一共可以取9个值,我们可以单独使用也可以通过“|”来合起来使用。下面我们将依次来解释这9个值得意思。

一、键盘状态

stateUnspecified
       这个属性值的意思是:未指定状态。我们如果在activity标签中没有设置android:windosSoftInputMode属性时,那么系统默认得键盘状态就是这个。系统会根据当前activity中的view来判断是否需要显示软键盘。这个是什么意思呢?就是说:系统不确定是否需要弹出软键盘的时候它不会自动弹出软键盘的。为什么说是不确定呢?因为有的时候系统会自动弹出软键盘,如:

<RelativeLayout 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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/show"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/hello_world"
android:textSize="20sp" />
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/show"
android:layout_marginTop="20dp"
android:hint="为获取到焦点"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

stateUnchanged
        这个属性值的意思是:状态不改变。啥意思呢?就是说上个页面的软键盘是啥状态,那么到这个页面的话软键盘状态不变和上个页面一样。如:上个页面时隐藏的,那么到了这个页面还是隐藏。反之,同理。

stateHidden
        这个属性值的意思是:隐藏状态。这个就是说,设置了这个属性值的话,软键盘是不显示出来的。

stateAlwaysHidden
        这个属性值的意思是:软键盘总是隐藏的。不管当前activity是否有获得焦点。

stateVisible
        这个属性值的意思是:显示软键盘,也可以说是强制性弹出软键盘。

stateAlwaysVisible
        这个属性值也是显示软键盘,那这个属性值与stateVisible有何区别呢?我们举个例子来说明这两者之间的区别。当我们在当前页面设置为“stateVisible”时,此时软键是显示出来的,然后我们跳转到下个页面,那么软键盘会消失,然后我们再回到上个页面时,软键盘不会显示出来,而我们如果在当前页面设置属性值为“stateAlwaysVisible”时,调到下个页面再返回到当前页面时,软键盘还是会显示出来的。这就是两者的区别。

二、窗口空间状态

adjustUnspecified
        这是默认的选项值,在这种情况下,系统会根据页面选择不同的模式。如果页面的布局含有ScrowView的话,系统会减少可以滚动的界面的大小,从而保证即使软键盘显示出来了也能看到所有的内容。如果页面布局不含有滚动控件,那么软键盘可能会覆盖掉一部分内容。

adjustResize
        这个属性值的意思是:activity窗口的大小会被调整来保证软键盘的显示空间。

adjustPan
        这个属性值的意思是:不会调整activity窗口的大小来保证软键盘的显示空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: