【问题标题】:What is the "state_focused" state for a Button?Button 的“state_focused”状态是什么?
【发布时间】:2012-01-13 14:55:34
【问题描述】:

我希望按钮背景在单击按钮后保持某种颜色,并在按下其他按钮时再次更改颜色。我认为这是“state_focused”状态。

但我的按钮似乎只有两种状态是按下或未按下。

我是否正确理解 state_focused 状态,还是我的 StateListDrawable(见下文)错误?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:state_pressed="false"><shape>
            <solid android:color="#00ff00" />
        </shape></item>
    <item android:state_pressed="true"><shape>
            <solid android:color="#ff0000" />
        </shape></item>
    <item><shape>
            <solid android:color="#0000ff" />
        </shape></item>

</selector>

【问题讨论】:

    标签: android android-button


    【解决方案1】:

    state_focused 是当按钮专注于使用 dpad 或轨迹球时。使用触摸时,视图通常不会显示焦点状态。

    【讨论】:

    • 谢谢 dmon。无论如何,您是否看到使用 StateListDrawable 来完成我想要的 Button?
    • 我相信你可以使用“focusableInTouchMode”,但开发者指南建议反对它:“Fousable in touch mode (see View.setFocusableInTouchMode) 是一个你可以自己设置的属性,无论是从代码还是从XML。但是,您应该谨慎使用它,并且只在非常特殊的情况下使用它,因为它破坏了与 Android UI 的正常行为的一致性。" 请参阅 developer.android.com/resources/articles/touch-mode.html
    • 我不禁觉得这并不能回答实际问题。
    • 这个答案仍然是最新的吗?许多手机现在都有一个接近传感器来判断用户的手指何时位于按钮上方。
    • 我在使用 d-pad 的 android 电视上使用 state_focused,但焦点处没有改变颜色。不适用于我的情况。我正在查看安卓电视模拟器。
    【解决方案2】:

    这里是按钮状态的示例:

    字体:http://developer.android.com/design/style/touch-feedback.html

    【讨论】:

    • 感谢 ademar111190,但此链接仅显示视图在不同状态下的外观。我的问题是如何让按钮在按下后保持某种颜色,直到按下其他按钮。
    【解决方案3】:

    https://developer.android.com/guide/topics/resources/drawable-resource.html

    android:state_pressed
    布尔值。如果在按下对象时(例如触摸/单击按钮时)应使用此项目,则为“true”;如果此项应在默认的非按下状态下使用,则为 "false"。

    android:state_focused
    布尔值。如果在对象具有输入焦点时(例如当用户选择文本输入时)应使用此项目,则为“true”;如果此项应在默认的非聚焦状态下使用,则为“false”。

    android:state_hovered
    布尔值。如果当对象被光标悬停时应使用此项目,则为“true”; "false" 如果此项应在默认的非悬停状态下使用。通常,此可绘制对象可能与用于“聚焦”状态的可绘制对象相同。
    在 API 级别 14 中引入。

    android:state_selected
    布尔值。如果在使用方向控件导航时(例如使用 d-pad 导航列表时)对象是当前用户选择,则应使用此项目;如果在未选择对象时应使用此项目,则为“false”。
    当焦点 (android:state_focused) 不够时(例如当列表视图具有焦点并且使用 d-pad 选择其中的项目时)使用选定状态。

    android:state_checkable
    布尔值。如果在对象可检查时应使用此项目,则为“true”;如果在对象不可检查时应使用此项目,则为“false”。 (仅当对象可以在可检查和不可检查小部件之间转换时才有用。)

    android:state_checked
    布尔值。如果在检查对象时应使用此项目,则为“true”;如果在取消选中对象时应该使用它,则为“false”。

    android:state_enabled
    布尔值。 "true" 如果在启用对象时应使用此项目(能够接收触摸/点击事件);如果在禁用对象时应该使用它,则为“false”。

    android:state_activated
    布尔值。 "true" 如果在对象被激活为持久选择时应使用此项目(例如在持久导航视图中“突出显示”先前选择的列表项);如果在对象未激活时使用,则为“false”。
    在 API 级别 11 中引入。

    android:state_window_focused
    布尔值。如果在应用程序窗口有焦点(应用程序在前台)时使用此项,则为“true”,如果在应用程序窗口没有焦点时(例如,如果通知阴影为下拉或出现对话框)。

    【讨论】:

    • 谢谢 Bourbon,但您有什么指导可以让按钮在按下后保持某种颜色,直到使用 StateListDrawable 按下其他按钮?
    • 你试过在onClick方法中做一个setPressed(true)吗?
    • 我还没有尝试过那个特定的方法,但是在代码中有很多方法可以做到这一点(而且我确实在运行时工作),我只是认为它会更干净如果我可以在 XML StateListDrawable 中做到这一点。
    • 嗯...试试 android:state_activated
    【解决方案4】:

    我知道很晚了,来自doco

    android:state_focused

    State value for StateListDrawable, set when a view has input focus.
    
    May be a boolean value, such as "true" or "false".
    

    根据我的测试,焦点是用户使用“下一个/上一个”用户界面(例如在软键盘或远程控制设备 (Android TV) 上)导航到用户界面元素时,或者用户触摸并按住时一个按钮而不释放它。我必须使用 state_pressed=true 和 state_focused=true 来呈现一个长按 UI 可绘制对象。

    【讨论】:

      【解决方案5】:

      要更改按钮的背景颜色并使其在被点击后保持不变,您只需:

      1. 创建 ColorStateSelector XML 文件
      2. 将按钮的“backgroundTint”属性设置为上述 XML 文件
      3. 将按钮的状态设置为所需的并在 ColorState 文件中定义

      以 Kotlin 和 Material Buttons 为例:

      ColorStateSelector 文件(res/color/buttons_color_state.xml):

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
          <!-- Red color for pressed state, the pressed state is changed back to normal button color after a certain short time, white in this case-->
          <item android:color="#FF1744" android:state_pressed="true" />
      
          <!-- Green color for this state, it's reserved for persistent color change-->
          <item android:color="#00E676" android:state_selected="true" />
      
          <!-- White for neutral default state-->
          <item android:color="@android:color/white" />
      
      </selector>
      

      布局 XML:

      <com.google.android.material.button.MaterialButton
                  android:id="@+id/buttonOption"
                  android:backgroundTint="@color/buttons_color_state.xml"
                  ... />
      

      Kotlin 文件:

      val buttonOption = view?.findViewById<MaterialButton>(R.id.buttonOption)
      
      // Implement any "if" checks or other control checks here if necessary
        buttonOption.isSelected = true
      // now the button is Green!
      
      

      祝你好运!

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 1970-01-01
        • 2023-04-07
        • 2019-06-20
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        相关资源
        最近更新 更多