android - 当其他 EditText 清除焦点时,阻止 EditText 获得焦点

标签 android focus

在我的 Activity 中,我得到了一些 EditText。当我从一个主题中移除焦点并使其不可聚焦时,焦点将被赋予另一个 EditText。我以这种方式移除焦点:

atxtTag.setFocusable(false);
atxtTag.setFocusableInTouchMode(false);
atxtTag.clearFocus();

我不知道如何将焦点转移到另一个 EditText 上。我试图通过在从第一个元素移除焦点后将焦点设置在另一个 View 上来实现这一点。所以我把焦点放在主要布局女巫是一个 ScrollView 但它没有用。

如何实现在不将焦点转移到另一个 EditText 的情况下移除对一个 EditText 的焦点?

谢谢。

最佳答案

clearFocus 方法 implementation clearFocusInternal 调用传递 true 作为 refocus 参数的值。这导致对第一个可聚焦元素的请求聚焦。因此,一个可能的解决方案是通过反射调用 clearFocusInternal(true, false);,但我还没有实际检查过。

深入研究requestFocus implementation我发现它检查 Root View 的 DescendantFocusability。我使用它构建了一个可行的解决方案:

ViewGroup rootView = (ViewGroup) atxtTag.getRootView();
int dfValue = rootView.getDescendantFocusability();
rootView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
atxtTag.clearFocus();
rootView.setDescendantFocusability(dfValue);

此外,清除焦点不会隐藏软键盘,因此您可能还需要隐藏它:

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(atxtTag.getWindowToken(), 0);

关于android - 当其他 EditText 清除焦点时,阻止 EditText 获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156709/

相关文章:

events - 优化在焦点上更新 ng-model 和 ng-style 的 AngularJS 指令

c# - 检测一组 WPF 控件丢失的焦点

java - 在 android studio 中生成签名的 apk 时未找到类型 `libcore.io.Memory`

java - 再次选中复选框后无法播放音乐

android - 当 Activity 在 Android 中启动时,如何阻止 EditText 获得焦点?

java - [某些] java 应用程序的焦点跟随鼠标行为不符合预期

javascript - 防止在点击输入时失去焦点

android - react 原生 : Android not setting width and height correct []

Android ACTiON_SEND MMS Intent 失败,返回 "Unable to attach. File not supported"

android - 在某些指定的矩形中绘制适合的文本