Android实现键盘弹出界面上移的实现思路

所属分类: 软件编程 / Android 阅读数: 70
收藏 0 赞 0 分享

1.首先说一下思路:

基本就是结合layout中ScrollView视图和AndroidManifest.xml中activity中的android:windowSoftInputMode属性配置实现;

2.要了解android:windowSoftInputMode相应的可以配置项:

activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性。
这个属性能影响两件事情:

 1.当有焦点产生时,软键盘是隐藏还是显示

 2.是否减少活动主窗口大小以便腾出空间放软键盘
windowSoftInputMode的设置必须是下面列表中的一个值,或一个”state…”值加一个”adjust…”值的组合。在任一组设置多个值——多个”state…”values,例如&mdash有未定义的结果。各个值之间用|分开。

例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >

在这设置的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆盖在主题中设置的值
各值的含义:

stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置

stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示

stateHidden:用户选择activity时,软键盘总是被隐藏

stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的

stateVisible:软键盘通常是可见的

stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态

adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示

adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间

adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

例如:

AndroidManifest.xml文件中界面对应的<activity>里加入
android:windowSoftInputMode="adjustPan"   键盘就会覆盖屏幕
android:windowSoftInputMode="stateVisible|adjustResize"   屏幕整体上移(结合ScrollView实现)
android:windowSoftInputMode="adjustPan|stateHidden" 软键盘弹出,界面布局不变,这是解决弹出软键盘,界面整体被压缩的方式(会导致整个界面上移动,显示效果不好)

3.具体实现

3.1定义ScrollView

<ScrollView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fitsSystemWindows="true"
  android:scrollbars="vertical">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/set_account_info"
      android:layout_marginTop="@dimen/business_management_title_top"
      style="@style/large_size_home_main_color_style"
      android:layout_gravity="center_horizontal"/>
    <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_marginTop="@dimen/add_confirm_top"
      android:focusable="true"
      android:focusableInTouchMode="true">
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/add_accout_bank_title"
        style="@style/cheque_collection_hint"
        android:layout_alignBaseline="@+id/accout_bank"
        android:text="@string/accout_bank"/>
      <EditText
        android:layout_width="@dimen/add_account_code_w"
        android:layout_marginTop="@dimen/common_gap"
        android:layout_toRightOf="@+id/add_accout_bank_title"
        style="@style/cheque_collection_et"
        android:id="@+id/accout_bank"/>
    </RelativeLayout>
    <Button
      android:id="@+id/confirm_add_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@style/common_btn_style"
      android:layout_gravity="center_horizontal"
      android:layout_marginTop="@dimen/add_confirm_top"
      android:text="@string/confirmed" />
  </LinearLayout>
</ScrollView>

即ScrollVIew中包裹EditText等内容;

3.2为AndroidManifest.xml文件中activity添加android:windowSoftInputMode="stateHidden|adjustResize"属性

<activity
  android:name=".ui.AddAccountActivity" android:windowSoftInputMode="stateHidden|adjustResize"
  android:screenOrientation="landscape"/>

说明:

stateHidden:进入Activity默认隐藏键盘,通常需要看见整个页面,用户需要输入时点击输入框;

adjustResize:界面调整大小,键盘留在底部,ScrollView内容可以滚动,这样就继续可以看到整个页面;

ScrollView通常不要设置android:fillViewport="true"(作用就是布满整个屏幕即使内容高度没有屏幕的高度)属性(看实际需要吧),android:fillViewport="true"导致界面无法滚动,API 19,21有这个问题,API 27没有这个问题,主要看你适配的版本和需求了;

3.3效果图如下

以上所述是小编给大家介绍的Android实现键盘弹出界面上移,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多