I like my UIs to be intuitive; each screen should naturally and unobtrusively guide the user on to the next step in the app. Barring that, I strive to make things as confusing and confounding as possible.

Just kidding :-)

I've got three TableRows, each containing a read-only and non-focusable EditText control and then a button to its right. Each button starts the same activity but with a different argument. The user makes a selection there and the sub-activity finishes, populating the appropriate EditText with the user's selection.

It's the classic cascading values mechanism; each selection narrows the available options for the next selection, etc. Thus I'm disabling both controls on each of the next rows until the EditText on the current row contains a value.

I need to do one of two things, in this order of preference:

  1. When a button is clicked, immediately remove focus without setting focus to a different button
  2. Set focus to the first button when the activity starts

The problem manifests after the sub-activity returns; the button that was clicked retains focus.

Re: #1 above - There doesn't appear to be a removeFocus() method, or something similar

Re: #2 above - I can use requestFocus() to set focus to the button on the next row, and that works after the sub-activity returns, but for some reason it doesn't work in the parent activity's onCreate().

I need UI consistency in either direction--either no buttons have focus after the sub-activity finishes or each button receives focus depending on its place in the logic flow, including the very first (and only) active button prior to any selection.

asked May 24 '11 at 23:17
InteXX

2,97942135

8 Answers

Using clearFocus() didn't seem to be working for me either as you found (saw in comments to another answer), but what worked for me in the end was adding:

<LinearLayout
android:id="@+id/my_layout"
android:focusable="true"
android:focusableInTouchMode="true" ...>

to my very top level Layout View (a linear layout). To remove focus from all Buttons/EditTexts etc, you can then just do

LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.my_layout);
myLayout.requestFocus();

Requesting focus did nothing unless I set the view to be focusable.

answered May 24 '11 at 23:29
actionshrimp

4,14331624

Old question, but I came across it when I had a similar issue and thought I'd share what I ended up doing.

The view that gained focus was different each time so I used the very generic:

View current = getCurrentFocus();
if (current != null) current.clearFocus();
answered Mar 18 '13 at 16:10
willlma

4,50911736
  1. You can use View.clearFocus().

  2. Use View.requestFocus() called from onResume().

answered May 24 '11 at 23:33
siliconeagle

5,23322232
<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();

I use this when already finished update profile info and remove all focus from EditText in my layout

====> Update: In parent layout content my EditText add line:

android:focusableInTouchMode="true"
answered Dec 2 '16 at 5:04
Ho Luong

15113

What about just adding android:windowSoftInputMode="stateHidden" on your activity in the manifest.

Taken from a smart man commenting on this: https://stackoverflow.com/a/2059394/956975

answered Nov 12 '14 at 18:35
marienke

1,69032258

First of all, it will 100% work........

  1. Create onResume() method.
  2. Inside this onResume() find the view which is focusing again and again by findViewById().
  3. Inside this onResume() set requestFocus() to this view.
  4. Inside this onResume() set clearFocus to this view.
  5. Go in xml of same layout and find that top view which you want to be focused and set focusable true and focusableInTuch true.
  6. Inside this onResume() find the above top view by findViewById
  7. Inside this onResume() set requestFocus() to this view at the last.
  8. And now enjoy......

I tried to disable and enable focusability for view and it worked for me (focus was reset):

focusedView.setFocusable(false);
focusedView.setFocusableInTouchMode(false);
focusedView.setFocusable(true);
focusedView.setFocusableInTouchMode(true);
answered Jun 26 '17 at 12:23
Serhiy

300411

You could try turning off the main Activity's ability to save its state (thus making it forget what control had text and what had focus). You will need to have some other way of remembering what your EditText's have and repopulating them onResume(). Launch your sub-Activities with startActivityForResult() and create an onActivityResult() handler in your main Activity that will update the EditText's correctly. This way you can set the proper button you want focused onResume() at the same time you repopulate the EditText's by using a myButton.post(new Runnable(){ run() { myButton.requestFocus(); } });

The View.post() method is useful for setting focus initially because that runnable will be executed after the window is created and things settle down, allowing the focus mechanism to function properly by that time. Trying to set focus during onCreate/Start/Resume() usually has issues, I've found.

Please note this is pseudo-code and non-tested, but it's a possible direction you could try.

answered Sep 13 '11 at 1:17
 

add a comment

https://stackoverflow.com/questions/6117967/how-to-remove-focus-without-setting-focus-to-another-control

How to remove focus without setting focus to another control?的更多相关文章

  1. e611. Setting Focus Traversal Keys for the Entire Application

    This example changes the focus traversal keys for the entire application. For an example of how to c ...

  2. e610. Setting Focus Traversal Keys in a Component

    When the focus is on a component, any focus traversal keys set for that component override the defau ...

  3. 键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )

    键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus ) 1.定义Keyboard Focus可以理解为物理焦点.就是整个桌面上可以响应键盘输入的地方,整个桌面在某个时刻只可能有一 ...

  4. PyQt(Python+Qt)学习随笔:键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 Qt中的焦点有键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )的区分,键 ...

  5. $( ).focus()与$( )[0].focus()区别

    $( #id).focus()与$( #id)[0].focus()没有区别,因为id必须是唯一的.如果同一页面出现多个相同的ID(这是不符合w3c规范的),$(#id)也只会拿到第一个该ID,后面的 ...

  6. View Focus的处理过程及ViewGroup的mFocused字段分析

    通过上篇的介绍,我们知道在对KeyEvent的处理中有非常重要的一环,那就是KeyEvent在focus view的path上自上而下的分发, 换句话说只有focus的view才有资格参与KeyEve ...

  7. 【移动端debug-4】iOS下setTimeout无法触发focus事件的解决方案

    开篇总结:其实目前无法解决这个bug. 这两天做项目遇到了这个case,项目需求是打开页面的时候,input元素自动弹起键盘.由于各种方面的考虑,我们希望通过setTimeout延时200毫秒让inp ...

  8. IE8 jq focus BUG

    jq的 .focus() 在IE8下面会有一些意想不到的BUG,下面是解决办法: 一.我做的项目中有些场景需要用到键盘的回车作为触发事件,然后把focus移到其他功能或者按钮上面,刚刚好这个按钮或者功 ...

  9. jquery失去焦点与获取焦点事件blur() focus()

    以前我们在js中写input各种事件时都会直接在input中写,昨天开始我开始全面使用jquery了,现在来谈一下我对jquery blur() focus()事件的学习笔记. 对于元素的焦点事件,我 ...

随机推荐

  1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本2(十)

    下面,是版本1. Hadoop MapReduce编程 API入门系列之挖掘气象数据版本1(一) 这篇博文,包括了,实际生产开发非常重要的,单元测试和调试代码.这里不多赘述,直接送上代码. MRUni ...

  2. 批量插入 SqlBulkCopy的测试

    关于SqlBulkCopy的测试 最近要做.net关于sql大量插入,找到了sqlbulkcopy(自己google下,应该很多说明了)这个好东西,于是测试下性能,用了三个方法对比: 1)直接用ado ...

  3. js+css模仿打字效果

    1.效果 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...

  4. Python 之 基础知识(五)

    一.变量 1.引用 id() 函数传参 与 返回值 都是传递保存的数据的引用 2.可变和不可变类型(变量的引用地址只在赋值语句后变化) 不可变类型 内存中的数据不允许被修改 数字类型 int,bool ...

  5. JS中的map

    定义和用法: map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值. map() 方法按照原始数组元素顺序依次处理元素. 注意: map() 不会对空数组进行检测. 注意: m ...

  6. win7不需要密码访问网络共享文件(转载)

    大家是不是觉得,共享一个文件夹后,每次输入密码都很麻烦呢?有没有不需要输入密码就可以访问共享文件的方法呢? 答案是肯定的,当然有,下面介绍下win7不需要密码访问网络共享文件 工具/原料 两台电脑在局 ...

  7. 【数据分析学习】Pandas学习记录

    import pandas as pd path = r'F:\数据分析专用\数据分析与机器学习\food_info.csv' with open(path, 'r') as f: data = pd ...

  8. 可横向滑动的vue tab组件

    示例 前端使用技术:框架->vue 组件>ly-tab一个用于移动端的可触摸滑动具有回弹效果的可复用Vue组件 ly-tab 介绍地址 ly-tab npm地址 使用步骤 1,引入包,定义 ...

  9. php-fpm优化处理

    php-fpm.conf有两个至关重要的参数:一个是”max_children”,另一个是”request_terminate_timeout”. 我的两个设置的值一个是”40″,一个是”900″,但 ...

  10. navicat Premium远程链接mysql报错

    1,报错1057,原来是没有远程权限连接mysql 2.打开my.ini文件,添加skip-grant-tables跳过验证 3.添加到path环境变量,前面是英文下的分号 4.切换到cmd,输入my ...