我有一个EditText
in an Activity
我希望当我打开它时它处于活动状态并且软键盘处于打开状态Activity
。这是我的xml
for EditText
:
<EditText
android:background="@null"
android:cursorVisible="true"
android:elegantTextHeight="true"
android:enabled="true"
android:focusable="true"
android:hint="Search"
android:id="@+id/editText11"
android:inputType="textNoSuggestions|textCapSentences"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:singleLine="true"
android:textColor="#000000"
android:textCursorDrawable="@null" />
我已经用过android:windowSoftInputMode="stateVisible"
对于我有这个的活动EditText
.
问题是,当我按back
一旦,键盘不会隐藏(理想情况下它在所有其他EditText
s) 当我按下back
再次,它关闭了Activity
。在第一back
按,我是not接到电话onBackPressed()
而在第二个back
按,我愿意。为什么会发生这种行为以及如何解决?
Edit我想要的是,如果键盘打开,按后退应关闭键盘,如果键盘未打开,则关闭活动。
尝试这个 ...
创建名为 Util 的类并放入以下代码
public static void hideSoftKeyboard(Activity activity) {
final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
if (activity.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
并调用 Activity 的 onBackPressed()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)