【发布时间】:2016-11-08 00:37:31
【问题描述】:
我有一个带有 EditText 的片段,并使用事务将其添加到布局中。但是如果我旋转到横向,软键盘就会消失。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getSupportFragmentManager().findFragmentById(R.id.fragmentContainer) == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, new FragmentWithEditText())
.commit();
}
}
}
我希望使用片段事务旋转后键盘状态保持不变。因为如果我不使用事务,而是直接在布局中添加一个片段,键盘并没有消失。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:tag="fragmentWithKeyboard"
android:name="com.test.FragmentWithEditText"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
我已经尝试使用android:windowSoftInputMode="stateUnchanged" 或android:configChanges="keyboardHidden|orientation",但没有帮助。
我还编写了一个具有此行为的示例应用程序https://github.com/anton9088/FragmentAndKeyboard
类似问题:
【问题讨论】:
-
使用这个 android:windowSoftInputMode="stateUnchanged|adjustResize"
-
android:windowSoftInputMode="stateUnchanged|adjustResize" 不工作,但如果我使用 stateAlwaysVisible 键盘出现在纵向,但不是在横向
-
@Radhey 奇怪的是,这仅在父 Activity 的布局中有 EditText 时才有效。如果片段中只有 EditText 并且它有焦点并显示键盘,则在方向更改后,键盘隐藏。
-
@user1275972 你有什么解决办法吗?
标签: android android-fragments android-softkeyboard android-orientation