片段交易后点击EditText消失
EditText disapears when clicked after fragment transaction
我的 EditText 工作得很好,直到您打开另一个片段并 return 回到第一个片段。单击 EditText 后,键盘弹出,看起来软键盘正在将片段的内容向上推到背景图像后面。
看起来像这样:
打开片段并再次关闭后:
我的搜索文本视图:
<EditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:background="@drawable/textfield"
android:digits="@string/digits"
android:drawableStart="@drawable/ic_search"
android:drawablePadding="10dp"
android:fontFamily="@font/opensans_normal"
android:hint="@string/search"
android:inputType="text"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/colorFont"
android:textColorHint="@color/colorFont"
android:textSize="15sp" />
当调用此方法时,错误发生:
fun addFilter() {
val topicList = mutableListOf<Topic?>()
topicAdapter = TopicAdapter(topicList, context, null, true)
if (CachedData.filter.isNotEmpty()) {
if (CachedData.filter[0] != null) {
filterList.visibility = View.VISIBLE
topicList.add(Topic(CachedData.filter[0], selected = true, clickable = true))
}
if (CachedData.filter[1] != null) {
filterList.visibility = View.VISIBLE
if (CachedData.filter[0] == null) {
topicAdapter.setSubselect(-1)
}
topicList.add(Topic(CachedData.filter[1], selected = true, clickable = true))
}
}
filterList.adapter = topicAdapter
filterList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
}
更新
只有在第一个片段中发生更改时才会发生这种情况。例如,如果我在第一个片段中将数据从 RecylerView 添加到数据集,则会发生错误
打开第二个片段时隐藏软键盘。使用此代码:
public void hideKeyboard(View view) {
if (view != null) {
view.clearFocus();
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
并在第一个片段的 onPause()
处调用 hideKeyboard()
方法,如下所示:
@Override
public void onPause() {
super.onPause();
if (yourEdittext!= null)
hideKeyboard(yourEdittext);
}
并且 windowSoftInputMode
到您的 activity 清单中,如下所示:
<activity
android:name=".YourActivity"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden|adjustPan">
</activity>
只需覆盖 activity 中的方法即可。它也会自动在其子片段中工作...
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (currentFocus != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
return super.dispatchTouchEvent(ev)
}
也可以在您的 Activity、Fragments 中使用以下实用函数来隐藏软键盘。
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
无论您在对话框片段 and/or activity 等中的代码如何,这都将关闭键盘
在Activity/Fragment中的用法:
hideKeyboard()
在manifest.xml
文件中设置android:windowSoftInputMode="adjustPan|adjustResize"
对应activity。
我的 EditText 工作得很好,直到您打开另一个片段并 return 回到第一个片段。单击 EditText 后,键盘弹出,看起来软键盘正在将片段的内容向上推到背景图像后面。
看起来像这样:
打开片段并再次关闭后:
我的搜索文本视图:
<EditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:background="@drawable/textfield"
android:digits="@string/digits"
android:drawableStart="@drawable/ic_search"
android:drawablePadding="10dp"
android:fontFamily="@font/opensans_normal"
android:hint="@string/search"
android:inputType="text"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:singleLine="true"
android:textColor="@color/colorFont"
android:textColorHint="@color/colorFont"
android:textSize="15sp" />
当调用此方法时,错误发生:
fun addFilter() {
val topicList = mutableListOf<Topic?>()
topicAdapter = TopicAdapter(topicList, context, null, true)
if (CachedData.filter.isNotEmpty()) {
if (CachedData.filter[0] != null) {
filterList.visibility = View.VISIBLE
topicList.add(Topic(CachedData.filter[0], selected = true, clickable = true))
}
if (CachedData.filter[1] != null) {
filterList.visibility = View.VISIBLE
if (CachedData.filter[0] == null) {
topicAdapter.setSubselect(-1)
}
topicList.add(Topic(CachedData.filter[1], selected = true, clickable = true))
}
}
filterList.adapter = topicAdapter
filterList.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
}
更新
只有在第一个片段中发生更改时才会发生这种情况。例如,如果我在第一个片段中将数据从 RecylerView 添加到数据集,则会发生错误
打开第二个片段时隐藏软键盘。使用此代码:
public void hideKeyboard(View view) {
if (view != null) {
view.clearFocus();
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
并在第一个片段的 onPause()
处调用 hideKeyboard()
方法,如下所示:
@Override
public void onPause() {
super.onPause();
if (yourEdittext!= null)
hideKeyboard(yourEdittext);
}
并且 windowSoftInputMode
到您的 activity 清单中,如下所示:
<activity
android:name=".YourActivity"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden|adjustPan">
</activity>
只需覆盖 activity 中的方法即可。它也会自动在其子片段中工作...
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (currentFocus != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
return super.dispatchTouchEvent(ev)
}
也可以在您的 Activity、Fragments 中使用以下实用函数来隐藏软键盘。
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
无论您在对话框片段 and/or activity 等中的代码如何,这都将关闭键盘
在Activity/Fragment中的用法:
hideKeyboard()
在manifest.xml
文件中设置android:windowSoftInputMode="adjustPan|adjustResize"
对应activity。