如何解决当设备旋转时,如何防止将Edittext聚焦回到第一个EditText
假设我有4个包含布局的linearlayout
view_edittext
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/il"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>
活动
<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/layoutET1"
layout="@layout/view_test"/>
<include
android:id="@+id/layoutET2"
layout="@layout/view_test"/>
<include
android:id="@+id/layoutET3"
layout="@layout/view_test"/>
<include
android:id="@+id/layoutET4"/>
</LinearLayout>
示例我正在填写表单,现在将edittext光标聚焦在layoutET3中,但是当我旋转设备时,edittext光标再次回到layoutET1。如何在设备旋转之前将Edittext光标放置在正确的位置(在这种情况下,请坚持layoutET3)?
我不能使用currfentFocus.id然后保存并还原方法,因为这会导致我在4个包含的布局之间使用相同的ID
解决方法
您必须在Java / Kotlin代码中执行某些操作,以使其发挥这种作用。旋转时焦点对我来说不会改变。
,尝试添加
android:configChanges="orientation|screenSize"
用于清单中的Activity
声明
这将导致onConfigurationChanged
方法调用而不是整个Activity
重新创建,因此应将注意力集中在位置上。关于configChanges
HERE
另一种方法是,在id
即将被销毁时,通过调用View
来获取getCurrentFocus()
当前关注的Activity
,并将此id
与{{ 1}},并在savedInstanceState
重新创建后恢复。 HERE中的一些指南,我认为这将是更好的方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。