UI界面被输入法遮挡问题

http://blog.csdn.net/moruna/article/details/7879069

1、在Android系统中,有时候在文本框中输入文字后,操作按钮被输入法遮挡了,不得不关闭输入法才可以继续操作。默认时时这样的,很麻烦。

2、于是,查了一些资料,终于找到方法。

先来看下代码与效果:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#ffffffff" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="horizontal"  
  12.         android:paddingBottom="30dp" >  
  13.   
  14.         <Button  
  15.             android:id="@+id/Date"  
  16.             android:layout_weight="1"  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content"  
  19.             android:textSize="16dp"  
  20.             android:gravity="center_vertical"  
  21.             android:singleLine="true"  
  22.             android:layout_marginTop="3dp"/>  
  23.   
  24.         <Button  
  25.             android:id="@+id/Time"  
  26.             android:layout_weight="1"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:textSize="16dp"  
  30.             android:gravity="center_vertical"  
  31.             android:singleLine="true"  
  32.             android:layout_marginTop="3dp" />  
  33.     </LinearLayout>  
  34.   
  35.     <EditText  
  36.         android:id="@+id/mText"  
  37.         android:layout_width="fill_parent"  
  38.         android:layout_height="100px"  
  39.         android:background="@drawable/shape"   
  40.         android:textColor="#aa000000"  
  41.         android:textSize="20dp"  
  42.         android:gravity="top"  
  43.         android:layout_margin="3dp"  
  44.         android:paddingLeft="10dp"  
  45.         android:paddingRight="10dp"/>  
  46.   
  47.     <LinearLayout  
  48.         android:layout_width="fill_parent"  
  49.         android:layout_height="fill_parent"  
  50.         android:orientation="horizontal" >  
  51.   
  52.         <Button  
  53.             android:id="@+id/set"  
  54.             android:layout_weight="1"  
  55.             android:layout_width="fill_parent"  
  56.             android:layout_height="wrap_content"  
  57.             android:layout_gravity="bottom"  
  58.             android:text="Set"  
  59.             android:textSize="20dp"  
  60.              />  
  61.   
  62.         <Button  
  63.             android:id="@+id/discard"  
  64.             android:layout_weight="1"  
  65.             android:layout_width="fill_parent"  
  66.             android:layout_height="wrap_content"  
  67.             android:layout_gravity="bottom"  
  68.             android:text="Cancel"  
  69.             android:textSize="20dp" />  
  70.     </LinearLayout>  
  71.   
  72. </LinearLayout>  
<?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属性中加一条:

  1. <activity  
  2.             android:name="com.ideal.studys.EditTime"  
  3.             android:label="@string/app_name"   
  4.             android:windowSoftInputMode="stateHidden|adjustResize" >  
  5.         </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主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值