解决弹出软键盘会将原有布局顶起(破坏原有布局解决方案)

本文介绍了解决Android应用中软键盘遮挡输入框的三种有效方法:通过代码设置、修改AndroidManifest.xml文件和使用ScrollView布局。这些方法能够帮助开发者优化用户体验,确保在输入时软键盘不会遮挡关键界面元素。

解决的方案大致有3种

方案一:

当你在你的activity中的oncreate中setContentView之前写上这个代码以后,软键盘会覆盖在屏幕上面,而不会把你的布局顶上去。代码如下:getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
方案二:

或者你也可以在清单AndroidManifest.xml文件中对应的<activity>里加入android:windowSoftInputMode="stateVisible|adjustResize",这样会让屏幕整体上移。如果加上的是android:windowSoftInputMode="adjustPan"这样键盘就会覆盖屏幕。
方法三:

把顶级的layout替换成ScrollView,或者说在顶级的Layout上面再加一层ScrollView的封装。这样就会把软键盘和输入框一起滚动了,软键盘会一直处于底部。

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

【一】当有焦点产生时,软键盘是隐藏还是显示

【二】是否减少活动主窗口大小以便腾出空间放软键盘

它的设置必须是下面列表中的一个值,或一个”state…”值加一个”adjust…”值的组合。在任一组设置多个值——多个”state…”values,例如&mdash有未定义的结果。各个值之间用|分开。例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >

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


各值的含义如下:

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

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

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

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

【5】stateVisible:软键盘通常是可见的

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

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

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

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

使用可滚动的布局(如ScrollView)包裹内容,并确保Button在ScrollView内。<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/close_black" android:clickable="true" android:focusable="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.042" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.068" /> <ImageView android:id="@+id/isp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_isp" android:clickable="true" android:focusable="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.958" app:layout_constraintTop_toTopOf="@id/close" app:layout_constraintVertical_bias="0.081" /> <TextView android:id="@+id/create_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_18" android:text="@string/create_account" android:textSize="@dimen/tpds_all_text_size_22" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.086" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/close" /> <com.tplink.design.text.TPTextViewDrawable android:id="@+id/text" style="@style/Widget.TPDesign.TextView.Secondary.Subtitle1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/email_ver" android:textColor="@color/grey_text" android:layout_marginHorizontal="@dimen/tpds_all_dp_16" android:layout_marginTop="@dimen/tpds_all_dp_10" app:layout_constraintTop_toBottomOf="@id/create_text" /> <com.tplink.design.text.TPTextField android:id="@+id/tf_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/e_mail" android:layout_marginTop="@dimen/tpds_all_dp_30" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginEnd="@dimen/tpds_all_dp_16" app:layout_constraintTop_toBottomOf="@id/text" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <ImageView android:id="@+id/region_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_region" android:layout_marginTop="@dimen/tpds_all_dp_10" android:layout_marginEnd="@dimen/tpds_all_dp_104" app:layout_constraintTop_toBottomOf="@id/tf_content" app:layout_constraintEnd_toEndOf="@id/tf_content" /> <TextView android:id="@+id/region" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/_region" android:textSize="@dimen/tpds_all_text_size_16" android:textColor="@color/tp" android:clickable="true" android:focusable="true" android:layout_marginStart="@dimen/tpds_all_dp_6" app:layout_constraintTop_toTopOf="@id/region_icon" app:layout_constraintStart_toEndOf="@id/region_icon" /> <!-- TODO 按钮无法顶起--> <Button android:id="@+id/btn_contained" style="?attr/materialButtonStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/tpds_all_dp_16" android:layout_marginEnd="@dimen/tpds_all_dp_16" android:layout_marginBottom="@dimen/tpds_all_dp_56" android:text="@string/_next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/already_have" android:clickable="true" android:focusable="true" android:gravity="center" android:textColor="@color/grey_text" android:textSize="@dimen/tpds_all_text_size_14" android:layout_marginBottom="@dimen/tpds_all_dp_20" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>应该怎么设置呢
最新发布
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值