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

时间:2021-05-19

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实现键盘弹出界面上移,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章