如何在不将焦点设置到另一个控件的情况下移除焦点? [英] How to remove focus without setting focus to another control?

查看:31
本文介绍了如何在不将焦点设置到另一个控件的情况下移除焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢我的用户界面直观;每个屏幕都应该自然而不引人注意地引导用户进入应用程序的下一步.除此之外,我会尽量让事情变得混乱和混乱.

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.

开个玩笑:-)

我有三个 TableRow,每个都包含一个只读且不可聚焦的 EditText 控件,然后在其右侧有一个按钮.每个按钮启动相同的活动,但具有不同的参数.用户在那里进行选择,子活动结束,用用户的选择填充适当的 EditText.

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.

这是经典的级联值机制;每个选择都缩小了下一个选择的可用选项,等等.因此,我将禁用下每一行的两个控件,直到当前行的 EditText 包含一个值.

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. 当一个按钮被点击时,立即移除焦点而不将焦点设置到不同的按钮
  2. 在活动开始时将焦点设置到第一个按钮

子活动返回后出现问题;被点击的按钮保持焦点.

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

回复:上面的 #1 - 似乎没有 removeFocus() 方法,或类似的东西

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

回复:上面的#2 - 我可以使用 requestFocus() 将焦点设置到下一行的按钮上,这在子活动返回后有效,但由于某种原因它没有t 在父活动的 onCreate() 中工作.

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().

我需要两个方向的 UI 一致性——在子活动完成后没有按钮具有焦点,或者每个按钮根据其在逻辑流中的位置获得焦点,包括在任何之前的第一个(也是唯一的)活动按钮选择.

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.

推荐答案

正如您所发现的那样,使用 clearFocus() 似乎对我不起作用(在另一个答案的评论中看到),但是在最后添加:

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.

这篇关于如何在不将焦点设置到另一个控件的情况下移除焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆