【问题标题】:Keyboard disappears on orientation change键盘在方向更改时消失
【发布时间】: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

类似问题:

Retain soft-input/IME state on Orientation change

Keyboard dismissed on rotation to landscape mode Android

【问题讨论】:

  • 使用这个 android:windowSoftInputMode="stateUnchanged|adjustResize"
  • android:windowSoftInputMode="stateUnchanged|adjustResize" 不工作,但如果我使用 stateAlwaysVisible 键盘出现在纵向,但不是在横向
  • @Radhey 奇怪的是,这仅在父 Activity 的布局中有 EditText 时才有效。如果片段中只有 EditText 并且它有焦点并显示键盘,则在方向更改后,键盘隐藏。
  • @user1275972 你有什么解决办法吗?

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


【解决方案1】:

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

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

【讨论】:

  • 问题不在焦点上,EditText在旋转前后都有焦点
  • @user1275972 但它在片段暂停期间失去焦点。这可能会触发键盘消失,因为在片段重新创建期间没有可编辑的小部件。但奇怪的是,如果 EditText 在 Activity 中,并且其清单中有 configChanges,它就可以工作。
【解决方案2】:

问题

问题是我们的视图应该将windowSoftInputMode 设置为stateUnchanged,这意味着:

当 Activity 出现时,软键盘会保持在它最后的任何状态,无论是可见还是隐藏。

在一个活动中,它可以通过简单地更改AndroidManifest.xml来获得:

<activity
    ...
    android:windowSoftInputMode="stateUnchanged"/>

很遗憾,它不适用于片段。

DialogFragment 的解决方案

如果您的片段是DialogFragment,您可以通过在您已经创建对话框时将此代码添加到您的onCreateDialog 方法来获得它:

Window window = dialog.getWindow();
if (window != null) {
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED);
}

【讨论】:

    【解决方案3】:

    在您的清单中更改以下行,我相信它会对您有所帮助

    <activity
    android:name=".YourActivityName"
    android:configChanges="orientation|screenSize">
    </activity>
    

    所以更换你的

    android:configChanges="keyboardHidden|orientation"android:configChanges="orientation|screenSize"

    【讨论】:

    • 它正在工作,但我更喜欢不要使用 configChanges 以避免手动处理方向更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    相关资源
    最近更新 更多