我有一个带有 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望使用片段事务旋转后键盘状态仍然不变。因为如果我不使用事务,而是直接在布局中添加片段,键盘不会消失。
<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>
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用android:windowSoftInputMode="stateUnchanged"or android:configChanges="keyboardHidden|orientation",但没有帮助。
我还写了一个具有这种行为的示例应用程序https://github.com/anton9088/FragmentAndKeyboard
类似问题:
android android-softkeyboard android-fragments android-orientation