方向改变时键盘消失 [英] Keyboard disappears on orientation change

查看:75
本文介绍了方向改变时键盘消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a fragment with EditText and add it into the layout using transactions. But if I rotate to landscape the soft keyboard disappears.

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

我希望使用片段事务旋转后键盘状态仍保持不变.因为如果我不使用事务,而是直接在布局中添加一个片段,则键盘不会消失.

I want keyboard state still unchanged after rotate using fragment transactions. Because if I don't use transactions, but add a fragment straight in the layout, the keyboard not disappeared.

<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",但没有帮助.

I already tried to use android:windowSoftInputMode="stateUnchanged" or android:configChanges="keyboardHidden|orientation", but didn't help.

我还写了一个具有这种行为的示例应用程序 https://github.com/anton9088/FragmentAndKeyboard

Also I wrote a sample app with this behavior https://github.com/anton9088/FragmentAndKeyboard

类似的问题:

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

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

推荐答案

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

Try to set your EditText's attribute freezesText value to true.

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

You can also add focus in onViewCreated callback manually

这篇关于方向改变时键盘消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆