Android:聚焦、启用、按下和选定状态有什么区别?

41

我查看了http://developer.android.com/reference/android/view/View.html,但是无法理解其中的许多内容,只能部分地理解“selected”状态。

有人能用一些具体的例子来解释它们之间的区别吗?如果我的问题不太明确,那么如果有人可以帮助我改进它,那就太好了,因为我不知道如何更清楚地提问。

先行致谢。

2个回答

45

启用 -> 用户可以进行交互。

禁用 -> 用户无法进行交互。

  • 如果您将鼠标悬停在小部件上,则它会获得焦点
  • 如果您对该小部件进行按下操作(半次点击),则它被按下
  • 如果您按下并释放鼠标,而鼠标在同一位置,则该小部件被选择

2
谢谢澄清!然而我对"focused"仍不太清楚:触摸屏中的"focussed"状态是如何工作的呢?是这样的吗:当我的手指在屏幕上移动时,无论我的手指在屏幕的哪个位置,那部分屏幕都处于焦点状态? - Engin Yapici
但是使用触摸输入时,你通常只需要关注所选状态,对吗?@EnginYapici 我认为当文本字段等待输入时,它是聚焦的。 - Stephan
使用触摸输入时,您需要注意按下和选择状态。如果触摸按下发生在小部件内部,而触摸松开发生在小部件外部,则不被视为已选择。 - 500865
1
实际上,"聚焦"也适用于触摸界面。如果屏幕上有多个半透明视图重叠(例如片段),则所有桌面范例都适用。此外,新的Android设备具有接近传感器,使得即使没有鼠标,"悬停"也变得有意义。按下/选择启用/禁用是过于简化的。 - Leo

7

聚焦 - (窗口,视图) 是键盘事件的目的地(是的,一些安卓设备有物理键盘),还有生成左上右下箭头键盘快捷键的"除臭球"。

已激活 - 已激活的小部件(视图)。例如,在多选列表中,选定的视图被激活。我认为在API 11中增加这个附加阶段的必要性是由于激活包含复选框的多选功能。因此,需要将选定状态和已选状态分开。

已选 - 仅适用于复选框和其他可选择的视图。

View状态的完整列表是(左侧为StateSet id,右侧为标志):

    R.attr.state_window_focused,    VIEW_STATE_WINDOW_FOCUSED,
    R.attr.state_selected,          VIEW_STATE_SELECTED,
    R.attr.state_focused,           VIEW_STATE_FOCUSED,
    R.attr.state_enabled,           VIEW_STATE_ENABLED,
    R.attr.state_pressed,           VIEW_STATE_PRESSED,
    R.attr.state_activated,         VIEW_STATE_ACTIVATED,
    R.attr.state_accelerated,       VIEW_STATE_ACCELERATED,
    R.attr.state_hovered,           VIEW_STATE_HOVERED,
    R.attr.state_drag_can_accept,   VIEW_STATE_DRAG_CAN_ACCEPT,
    R.attr.state_drag_hovered,      VIEW_STATE_DRAG_HOVERED

同时参见:

/**
 * Changes the activated state of this view. A view can be activated or not.
 * Note that activation is not the same as selection.  Selection is
 * a transient property, representing the view (hierarchy) the user is
 * currently interacting with.  Activation is a longer-term state that the
 * user can move views in and out of.  For example, in a list view with
 * single or multiple selection enabled, the views in the current selection
 * set are activated.  (Um, yeah, we are deeply sorry about the terminology
 * here.)  The activated state is propagated down to children of the view it
 * is set on.
 *
 * @param activated true if the view must be activated, false otherwise
 */
public void setActivated(boolean activated)



/**
 * Dispatch a key event to the next view on the focus path. This path runs
 * from the top of the view tree down to the currently focused view. If this
 * view has focus, it will dispatch to itself. Otherwise it will dispatch
 * the next node down the focus path. This method also fires any key
 * listeners.
 *
 * @param event The key event to be dispatched.
 * @return True if the event was handled, false otherwise.
 */
public boolean dispatchKeyEvent(KeyEvent event)

嗯,是的,我们对这里的术语深感抱歉。 - Langusten Gustel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接