我有一个带有 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
类似问题:
最佳答案
尝试设置 EditText 的属性 freezesText值为真。
你也可以在 onViewCreated 回调中手动添加焦点
关于android - 方向改变时键盘消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40477115/