1.首先说一下思路:

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

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

activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性。
这个属性能影响两件事情:
 1.当有焦点产生时,软键盘是隐藏还是显示
 2.是否减少活动主窗口大小以便腾出空间放软键盘
windowSoftInputMode的设置必须是下面列表中的一个值,或一个”state…”值加一个”adjust…”值的组合。在任一组设置多个值——多个”state…”values,例如&mdash有未定义的结果。各个值之间用|分开。
例如:
在这设置的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆盖在主题中设置的值
各值的含义:
stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
stateHidden:用户选择activity时,软键盘总是被隐藏
stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
stateVisible:软键盘通常是可见的
stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间

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

例如:
AndroidManifest.xml文件中界面对应的里加入
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实现键盘弹出界面上移_第1张图片

参考:

https://blog.csdn.net/baidu_31093133/article/details/70848288

https://blog.csdn.net/u011622479/article/details/51161717

                                                                                               

更多相关文章

  1. android 键盘设置详解
  2. Android保持屏幕常亮的三种方法
  3. Android 手动设置屏幕方向后不能自动转屏问题
  4. android 监听手机屏幕唤醒和睡眠广播
  5. Android 软键盘弹出 【遮挡输入框】【遮挡RecyclerView】的问题
  6. 在Android中销毁当前Fragment界面。
  7. android的frameworks层键盘事件处理流程分析
  8. Android自适应不同分辨率或不同屏幕大小
  9. Android 解屏幕锁与点亮屏幕

随机推荐

  1. android API key 如何获取
  2. 详解Android获取系统内核版本的方法与实
  3. Android源码在线阅读
  4. Android应用程序的调试方法
  5. Android第一行代码(第二版)
  6. Android(安卓)Bundle类
  7. android tips:从资源文件中读取文件流显
  8. Android调节屏幕亮度工具类BrightnessUti
  9. Android(安卓)dp方式的屏幕适配工具使用(
  10. Android(安卓)设置ProgressBar 的颜色