温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android如何实现键盘弹出界面上移

发布时间:2021-04-17 10:52:25 来源:亿速云 阅读:1529 作者:小新 栏目:移动开发

小编给大家分享一下Android如何实现键盘弹出界面上移,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

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"              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"                  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"                  android:id="@+id/accout_bank"/>     </RelativeLayout>     <Button       android:id="@+id/confirm_add_btn"       android:layout_width="wrap_content"       android:layout_height="wrap_content"              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如何实现键盘弹出界面上移”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI