How to remove focus without setting focus to another control?
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:
- When a button is clicked, immediately remove focus without setting focus to a different button
- 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.
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.
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();
You can use
View.clearFocus().Use
View.requestFocus()called fromonResume().
<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"
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
First of all, it will 100% work........
- Create
onResume()method. - Inside this
onResume()find the view which is focusing again and again byfindViewById(). - Inside this
onResume()setrequestFocus()to this view. - Inside this
onResume()setclearFocusto this view. - Go in xml of same layout and find that top view which you want to be focused and set
focusabletrue andfocusableInTuchtrue. - Inside this
onResume()find the above top view byfindViewById - Inside this
onResume()setrequestFocus()to this view at the last. - 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);
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.
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?的更多相关文章
- 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 ...
- 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 ...
- 键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )
键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus ) 1.定义Keyboard Focus可以理解为物理焦点.就是整个桌面上可以响应键盘输入的地方,整个桌面在某个时刻只可能有一 ...
- PyQt(Python+Qt)学习随笔:键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 Qt中的焦点有键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )的区分,键 ...
- $( ).focus()与$( )[0].focus()区别
$( #id).focus()与$( #id)[0].focus()没有区别,因为id必须是唯一的.如果同一页面出现多个相同的ID(这是不符合w3c规范的),$(#id)也只会拿到第一个该ID,后面的 ...
- View Focus的处理过程及ViewGroup的mFocused字段分析
通过上篇的介绍,我们知道在对KeyEvent的处理中有非常重要的一环,那就是KeyEvent在focus view的path上自上而下的分发, 换句话说只有focus的view才有资格参与KeyEve ...
- 【移动端debug-4】iOS下setTimeout无法触发focus事件的解决方案
开篇总结:其实目前无法解决这个bug. 这两天做项目遇到了这个case,项目需求是打开页面的时候,input元素自动弹起键盘.由于各种方面的考虑,我们希望通过setTimeout延时200毫秒让inp ...
- IE8 jq focus BUG
jq的 .focus() 在IE8下面会有一些意想不到的BUG,下面是解决办法: 一.我做的项目中有些场景需要用到键盘的回车作为触发事件,然后把focus移到其他功能或者按钮上面,刚刚好这个按钮或者功 ...
- jquery失去焦点与获取焦点事件blur() focus()
以前我们在js中写input各种事件时都会直接在input中写,昨天开始我开始全面使用jquery了,现在来谈一下我对jquery blur() focus()事件的学习笔记. 对于元素的焦点事件,我 ...
随机推荐
- SwiftUI 官方教程(六)
6. 在列表和详情之间设置导航 虽然列表已经能显示了,但是我们还不能通过点击单个地标来查看地标详情页面.SwiftUI教程 把 list 嵌入一个 NavigationView 中,并把每个 row ...
- POJ 3122 二分
大致题意: 就是公平地分披萨pie 我生日,买了n个pie,找来f个朋友,那么总人数共f+1人 每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就 ...
- 单件模式(Singleton)C++实现
意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 实用性:1.当类只能有一个实例而且客户可以从一个众所周知的访问点访问它. 2.当这个唯一的实例应该是通过子类可扩展的,并且客户应该无需更改 ...
- Codeforces Round #455
Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...
- 【Oracle】非RMAN恢复数据文件、控制文件
实验环境:OEL 5.6 oracle 11g(11.2.0.4.0) 注意: system表空间数据文件不能在线recover,需要启动到mount状态再recover: undo表空间数据文件可以 ...
- IAAS: IT公司去IOE-Alibaba系统构架解读
从Hadoop到自主研发,技术解读阿里去IOE后的系统架构 原地址:...................... 云计算阿里飞天 摘要:从IOE时代,到Hadoop与飞天并行,再到飞天单集群5000节 ...
- deeplearning4j – 分布式DL开源项目
原文链接:http://www.52ml.net/16157.html Deeplearning4j is the first commercial-grade deep learning libra ...
- ZBrush关于遮罩的一些操作
本文讨论使用ZBrush®软件如何在屏幕上创建遮罩和操纵遮罩. 1. 绘制遮罩 按下Ctrl键你就能够在你的模型上绘制遮罩(笔刷的笔划的开始和结束都必须在模型上),默认情况下,遮罩区域在模型上显示为一 ...
- python tips:类与实例的属性问题
实例是具象化的类,它可以作为类访问所有静态绑定到类上的属性,包括类变量与方法,也可以作为实例访问动态绑定到实例上的属性. 实例1: class A: work = list("hello&q ...
- 洛谷P3275 [SCOI2011]糖果_差分约束_判负环
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...
android
