现在的位置: 首页 > 综合 > 正文

防止UI界面被输入法遮挡(画面随输入法自适应)

2014年02月08日 ⁄ 综合 ⁄ 共 5648字 ⁄ 字号 评论关闭

相信用过Android手机的朋友都知道,有时候在文本框中输入文字后,操作按钮被输入法遮挡了,不得不关闭输入法才可以继续

比如下面这个画面:

 

画面布局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/ll2" android:orientation="vertical"  
  4.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  5.     <CheckBox android:id="@+id/widget57" android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:text="@string/location"  
  7.         android:layout_gravity="right">  
  8.     </CheckBox>  
  9.     <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" android:layout_weight="1"  
  11.         android:textColorHighlight="#cccccc" android:hint="你想说什么?"  
  12.         android:gravity="top" android:textSize="18sp">  
  13.     </EditText>  
  14.     <Button android:id="@+id/widget53" android:layout_width="100px"  
  15.             android:layout_height="wrap_content" android:text="@string/share"  
  16.             android:layout_gravity="right"/>  
  17. </LinearLayout>  

 

 

如果不做任何操作,那么点击文本框后的效果肯定是下图:

 

此时,【共享】按钮被输入法挡住了,必须关闭输入法才可以操作了。

 

 

有的朋友会说,可以在布局外面再加一个ScrollView,这样的画,UI布局就和输入法分离了,输入法出现后,上面还可以滚动。

 

但是这样的效果好吗? 我们来看一下效果(布局外加ScrollView的效果):

 

画面布局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4. android:orientation="vertical">  
  5. <LinearLayout   
  6.     android:id="@+id/ll2" android:orientation="vertical"  
  7.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  8.     <CheckBox android:id="@+id/widget57" android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" android:text="@string/location"  
  10.         android:layout_gravity="right">  
  11.     </CheckBox>  
  12.     <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  13.         android:layout_height="400px" android:layout_weight="1"  
  14.         android:textColorHighlight="#cccccc" android:hint="你想说什么?"  
  15.         android:gravity="top" android:textSize="18sp">  
  16.     </EditText>  
  17.     <Button android:id="@+id/widget53" android:layout_width="100px"  
  18.             android:layout_height="wrap_content" android:text="@string/share"  
  19.             android:layout_gravity="right"/>  
  20. </LinearLayout>  
  21. </ScrollView>  

 

 

从图中可以看出,上面部分右侧有一个滚动条,用户可以通过从下滚动来点击按钮。但是个人觉得,这样还不如直接关闭输入法来点击按钮来的方便!

 

那么有没有更好的办法呢? 答案是有!

先看一下这个更好的方法是什么效果:

 

 

从图中可以看出,UI布局也与输入法分离了,同时EditText区域自动缩小了。这样的话,即不影响用户输入,也不影响用户进一步操作!

而且即使打开了输入法,用户也可以看到UI全貌,感觉比较舒服、友好。

 

下面就说一下,这个效果是如何做到的.

 

其实答案很简单,不需要更改局部文件,而是修改AndroidManifest.xml文件,在Activity属性中加一条:

  1. android:windowSoftInputMode="adjustResize"  

 

 

AndroidManifest.xml修改前后比较: 

 

该属性还有一些其他值,大家可以上网查一下。 (这边给个链接: http://blog.csdn.net/liluo1217/archive/2011/02/14/6184169.aspx)

相信用过Android手机的朋友都知道,有时候在文本框中输入文字后,操作按钮被输入法遮挡了,不得不关闭输入法才可以继续

比如下面这个画面:

 

画面布局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/ll2" android:orientation="vertical"  
  4.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  5.     <CheckBox android:id="@+id/widget57" android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:text="@string/location"  
  7.         android:layout_gravity="right">  
  8.     </CheckBox>  
  9.     <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" android:layout_weight="1"  
  11.         android:textColorHighlight="#cccccc" android:hint="你想说什么?"  
  12.         android:gravity="top" android:textSize="18sp">  
  13.     </EditText>  
  14.     <Button android:id="@+id/widget53" android:layout_width="100px"  
  15.             android:layout_height="wrap_content" android:text="@string/share"  
  16.             android:layout_gravity="right"/>  
  17. </LinearLayout>  

 

 

如果不做任何操作,那么点击文本框后的效果肯定是下图:

 

此时,【共享】按钮被输入法挡住了,必须关闭输入法才可以操作了。

 

 

有的朋友会说,可以在布局外面再加一个ScrollView,这样的画,UI布局就和输入法分离了,输入法出现后,上面还可以滚动。

 

但是这样的效果好吗? 我们来看一下效果(布局外加ScrollView的效果):

 

画面布局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4. android:orientation="vertical">  
  5. <LinearLayout   
  6.     android:id="@+id/ll2" android:orientation="vertical"  
  7.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  8.     <CheckBox android:id="@+id/widget57" android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" android:text="@string/location"  
  10.         android:layout_gravity="right">  
  11.     </CheckBox>  
  12.     <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  13.         android:layout_height="400px" android:layout_weight="1"  
  14.         android:textColorHighlight="#cccccc" android:hint="你想说什么?"  
  15.         android:gravity="top" android:textSize="18sp">  
  16.     </EditText>  
  17.     <Button android:id="@+id/widget53" android:layout_width="100px"  
  18.             android:layout_height="wrap_content" android:text="@string/share"  
  19.             android:layout_gravity="right"/>  
  20. </LinearLayout>  
  21. </ScrollView>  

 

 

从图中可以看出,上面部分右侧有一个滚动条,用户可以通过从下滚动来点击按钮。但是个人觉得,这样还不如直接关闭输入法来点击按钮来的方便!

 

那么有没有更好的办法呢? 答案是有!

先看一下这个更好的方法是什么效果:

 

 

从图中可以看出,UI布局也与输入法分离了,同时EditText区域自动缩小了。这样的话,即不影响用户输入,也不影响用户进一步操作!

而且即使打开了输入法,用户也可以看到UI全貌,感觉比较舒服、友好。

 

下面就说一下,这个效果是如何做到的.

 

其实答案很简单,不需要更改局部文件,而是修改AndroidManifest.xml文件,在Activity属性中加一条:

  1. android:windowSoftInputMode="adjustResize"  

 

 

AndroidManifest.xml修改前后比较: 

 

该属性还有一些其他值,大家可以上网查一下。 (这边给个链接: http://blog.csdn.net/liluo1217/archive/2011/02/14/6184169.aspx)

抱歉!评论已关闭.