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

最新文章

  1. C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 多系统开发接口 - 苹果客户端开发接口
  2. 什么是SEM?
  3. JavaScript基于时间的动画算法
  4. Linux GDB Debug
  5. JS跨域方法及原理
  6. vs2010 mvc3创建的razor引擎模板页,子页面引用后出现当前上下文中不存在名称“ViewBag”
  7. jquery checkBox的问题
  8. 反射自动填充model
  9. [上传下载] C#FileDown文件下载类 (转载)
  10. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3
  11. Python查询SQLserver数据库备份(抛砖引玉)
  12. 2018-03-03-解决win下凭据删除不干净而无法登录共项目录的问题
  13. Leetcode_58_Length of Last Word
  14. navicat为mysql建立索引
  15. SQLServer之创建显式事务
  16. react 表单受控和非受控
  17. LeetCode题解之Binary Tree Pruning
  18. sublime text3最常用快捷键
  19. 清理MVC4 Internaet 项目模板清理
  20. Java输出错误信息与调试信息

热门文章

  1. BZOJ 2002 LCT板子题
  2. Ubuntu16.04系统下汉字显示为方框解决办法(图文详解)
  3. Json转换成DataTable
  4. &lt;a&gt;和&lt;table&gt;标签的应用
  5. tomcat注册windows服务
  6. apache出现You don&#39;t have permission to access / on this server提示的解决方法
  7. FBX骨骼坐标系与模型坐标系的关系
  8. lsync 负载实现代码双向同步
  9. 浅谈[^&gt;]在正则中的2种用法
  10. P1828 香甜的黄油 Sweet Butter (spfa)