引用:http://blog.csdn.net/hmg25/article/details/6246138

在做登录和注册页面的时候,经常会遇到诸如软键盘挡住输入框的情况,android为此提供了一系列的的配置参数供选择,你可以在androidmanufist.xml的对应Activity的windowSoftInputMode属性中选择如下4者之一进行配置(紫色字):

<activity android:name=".LoginAc"

android:label="@string/app_name"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

四个参数意思如下:

SOFT_INPUT_ADJUST_NOTHING: 不调整(输入法完全直接覆盖住,未开放此参数)

SOFT_INPUT_ADJUST_PAN: 把整个Layout顶上去露出获得焦点的EditText,不压缩多余空间,见图1

SOFT_INPUT_ADJUST_RESIZE: 整个Layout重新编排,重新分配多余空间,见图2

SOFT_INPUT_ADJUST_UNSPECIFIED: 系统自己根据内容自行选择上两种方式的一种执行(默认配置)

这里的多余空间指的是控件们通过weight分配机制得到的额外空间。

图1

图2

图3

代码实现方式为:

[java] view plain copy
  1. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

但是,这样的配置方法一般都很难完全满足需要,有得应用会做得比较好,让顶上去的Layout能够通过scrollbar滚动。这种解决方法网上有各种介绍,本人也是第一时间从网上找解决方法参考,但最终发现都并未把原理说清,而且大多数有错误,或者有多余配置,于是,我从android系统中源码中找参考案例,在Email应用中,找到了我想要的。效果如图4,5。

图4

图5

其对应的Activity是AccountSetupBasics.java,对应的xml文件为account_setup_basics.xml。

来学习下它的xml写法:

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
[java] view plain copy
  1. <ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:fillViewport="true"
  5. android:scrollbarStyle="outsideInset">
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:orientation="vertical">
  10. <LinearLayout
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1"
  14. android:orientation="vertical">
  15. <TextView
  16. android:id="@+id/instructions"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="10dip"
  20. android:textSize="20sp"
  21. android:text="@string/accounts_welcome"
  22. android:textColor="?android:attr/textColorPrimary"/>
  23. <View
  24. android:layout_width="fill_parent"
  25. android:layout_height="0dip"
  26. android:layout_weight="1"/>
  27. <EditText
  28. android:id="@+id/account_email"
  29. android:hint="@string/account_setup_basics_email_hint"
  30. android:inputType="textEmailAddress"
  31. android:imeOptions="actionNext"
  32. android:layout_height="wrap_content"
  33. android:layout_width="fill_parent"/>
  34. <EditText
  35. android:id="@+id/account_password"
  36. android:hint="@string/account_setup_basics_password_hint"
  37. android:inputType="textPassword"
  38. android:imeOptions="actionDone"
  39. android:layout_height="wrap_content"
  40. android:layout_width="fill_parent"
  41. android:nextFocusDown="@+id/next"/>
  42. <CheckBox
  43. android:id="@+id/account_default"
  44. android:layout_height="wrap_content"
  45. android:layout_width="fill_parent"
  46. android:text="@string/account_setup_basics_default_label"
  47. android:visibility="gone"/>
  48. <View
  49. android:layout_width="fill_parent"
  50. android:layout_height="0dip"
  51. android:layout_weight="1"/>
  52. </LinearLayout>
  53. <RelativeLayout
  54. android:layout_width="fill_parent"
  55. android:layout_height="54dip"
  56. android:background="@android:drawable/bottom_bar">
  57. <Button
  58. android:id="@+id/manual_setup"
  59. android:text="@string/account_setup_basics_manual_setup_action"
  60. android:layout_height="wrap_content"
  61. android:layout_width="wrap_content"
  62. android:minWidth="@dimen/button_minWidth"
  63. android:layout_alignParentLeft="true"
  64. android:layout_centerVertical="true"/>
  65. <Button
  66. android:id="@+id/next"
  67. android:text="@string/next_action"
  68. android:layout_height="wrap_content"
  69. android:layout_width="wrap_content"
  70. android:minWidth="@dimen/button_minWidth"
  71. android:drawableRight="@drawable/button_indicator_next"
  72. android:layout_alignParentRight="true"
  73. android:layout_centerVertical="true"/>
  74. </RelativeLayout>
  75. </LinearLayout>
  76. </ScrollView>

1 它完全把ScrollView作为了一个根Layout,而不是网上好多文章写的在一个Linearlayout里面嵌入一个ScrollView(貌似这种是行不通的)。

然后把我们原来的根Layout搬入ScrollView(ScrollView只能有一个子控件),我查了下androidmanifist.xml和代码,未做任何以上2种方法的配置。

2 它定义了2个0dip的View帮助分配空间(设置其weight吃掉剩余空间,保证输入框处于界面中心位置),可以猜测出这里系统调用的是SOFT_INPUT_ADJUST_RESIZE参数,当所有有实际内容的控件空间总和超出特定范围时,ScrollView开始发挥作用。

如此,完美的解决我们遇到的问题。

另外,网上有人说想用SOFT_INPUT_ADJUST_RESIZE,但又不希望背景图片被压缩,只要按如上方法把Linearlayout的背景图片设置好即可。

int SOFT_INPUT_ADJUST_NOTHING

Adjustment option forsoftInputMode:

set to have a window not adjust for a shown input method.

int SOFT_INPUT_ADJUST_PAN

Adjustment option forsoftInputMode:

set to have a window pan when an input method is shown,so it doesn't need to deal with resizing but just panned by

the framework to ensure the current input focus is visible.

int SOFT_INPUT_ADJUST_RESIZE

Adjustment option forsoftInputMode:

set to allow the window to be resized when an input method is shown,so that its contents are not covered by the input method.

int SOFT_INPUT_ADJUST_UNSPECIFIED

Adjustment option forsoftInputMode:

nothing specified.

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. Python技巧匿名函数、回调函数和高阶函数
  3. python list.sort()根据多个关键字排序的方法实现
  4. 三、安卓UI学习(1)
  5. android用户界面之按钮(Button)教程实例汇
  6. 在Fragment中设置控件点击方法,执行失败。
  7. android中文api(89)——ViewManager
  8. TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity
  9. Android调用天气预报的WebService简单例子

随机推荐

  1. android鍩轰簬tcpdump鐨勬暟鎹寘鎹曡幏
  2. Android(安卓)Application类的详细介绍
  3. android中的HandlerThread类的学习
  4. Android系统做了哪些优化?
  5. Android的Handler总结
  6. android、IOS 基于webview 与 HTML 的集
  7. android R 文件生成不了
  8. 从零搭建 repo 服务器
  9. Android线程优先级设置方法
  10. android笔记