【问题标题】:Android remove focus from all ViewsAndroid从所有视图中删除焦点
【发布时间】:2016-06-12 17:09:05
【问题描述】:

这是一个相当简单的问题,但我不敢相信我找到的所有答案都不起作用。

我有一个包含两个EditText 的布局,窗口底部只是一些剩余的空白区域。当用户单击窗口的空白部分时,我希望每个 EditText 都失去焦点。所以我的猜测是我必须在根ViewOnTouchListener中放一些东西:

rootView.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
            // Put something here
    }
});

我已经尝试了几件事:

  • 按照某处的建议从布局View 请求焦点实际上是将焦点放在EditTexts 之一。

  • 我还尝试将setFocusableEditTexts 都设置为false,但之后我无法将它们放回true

  • 我也尝试了简单的rootView.clearFocus(),但它也将焦点返回给EditText

编辑:我的 EditText 框是多行的,所以我不能使用“完成”键盘按钮,因为我需要返回一个。

【问题讨论】:

  • 您是否在根视图上尝试过android:focusableInTouchMode="true"?请同时发布您的布局文件。
  • rootView 是你的editText吗?因为你需要在你的editText上调用clearFocus()
  • 顺便说一句,如果 editText 是您的 only 视图,则此 clearFocus() 不起作用,clearFocus() 将焦点返回到第一个焦点视图。

标签: android android-edittext focus


【解决方案1】:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/ll_root_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

LinearLayout llRootView = findViewBindId(R.id.ll_root_view);
llRootView.clearFocus();

我在完成更新个人资料信息并从我的布局中的 EditText 中删除所有焦点时使用它

====> 更新:在父布局内​​容中我的 EditText 添加行:

android:focusableInTouchMode="true"

【讨论】:

  • 老实说,这是我在整个堆栈溢出中找到的最佳答案。非常感谢您的帮助。
【解决方案2】:

在 Kotlin 中:

view?.clearFocus()

在 Java 中:

getView().clearFocus();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2011-05-05
    • 2015-12-04
    相关资源
    最近更新 更多