android - 方向改变时键盘消失

标签 android android-fragments android-softkeyboard android-orientation

我有一个带有 EditText 的 fragment ,并使用事务将其添加到布局中。但是,如果我旋转到横向,软键盘就会消失。

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();
        }
    }
}

我希望在使用 fragment 事务旋转后键盘状态仍然不变。因为如果我不使用事务,而是直接在布局中添加一个 fragment ,键盘就不会消失。

<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

类似问题:

Retain soft-input/IME state on Orientation change

Keyboard dismissed on rotation to landscape mode Android

最佳答案

尝试设置 EditText 的属性 freezesText值为真。

你也可以在 onViewCreated 回调中手动添加焦点

关于android - 方向改变时键盘消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40477115/

相关文章:

android - 验证应用程序是否进入后台或 fragment 将被销毁

android - fragment 中操作栏中的搜索 View

android - 如何在触摸微调器时隐藏虚拟键盘

android - 如何重启onCreateInputView()?

android - 两个或多个类可以向同一个 IntentService 发送数据吗?

android - Android异常运行时无命令输出:'am start <应用程序/应用程序>

android - Firebase + RoboElectric : java. lang.IllegalStateException:名称为 [DEFAULT] 的 FirebaseApp 不存在

java - Android:在 Switch 语句中嵌套 for 循环以获取 ArrayList 的总和

android - 我如何用 fragment 布局填充我的内容布局

android - 适用于 Android 平台的 Flash API