方向改变时键盘消失 [英] Keyboard disappears on orientation change
问题描述
我有一个带有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
类似的问题:
推荐答案
尝试设置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屋!