小编use*_*972的帖子

键盘在方向改变时消失

我有一个带有 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

类似问题:

在方向改变时保持软输入/IME 状态

旋转到横向模式 Android 时关闭键盘

android android-softkeyboard android-fragments android-orientation

6
推荐指数
1
解决办法
1446
查看次数