What is the “state_focused” state for a Button?

痞子三分冷 提交于 2019-11-28 16:52:29

state_focused is whenever the button is focused on using a dpad or the trackball. Views don't generally show a focused state when using touch.

here a example of buttons states:

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

Bourbon

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

android:state_pressed
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.

android:state_focused
Boolean. "true" if this item should be used when the object has input focus (such as when the user selects a text input); "false" if this item should be used in the default, non-focused state.

android:state_hovered
Boolean. "true" if this item should be used when the object is being hovered by a cursor; "false" if this item should be used in the default, non-hovered state. Often, this drawable may be the same drawable used for the "focused" state.
Introduced in API level 14.

android:state_selected
Boolean. "true" if this item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad); "false" if this item should be used when the object is not selected.
The selected state is used when focus (android:state_focused) is not sufficient (such as when list view has focus and an item within it is selected with a d-pad).

android:state_checkable
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)

android:state_checked
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.

android:state_enabled
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.

android:state_activated
Boolean. "true" if this item should be used when the object is activated as the persistent selection (such as to "highlight" the previously selected list item in a persistent navigation view); "false" if it should be used when the object is not activated.
Introduced in API level 11.

android:state_window_focused
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).

I know it's late, from the 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".

From my testing, focus is when the user has navigated to the UI element using a "next/previous" UI like on a soft keyboard, or a remote control device (Android TV), or when the user touches and holds down a button without releasing it. I had to use state_pressed=true and state_focused=true to present a long-pressed UI drawable.

To change a Button's background color and make it persistent after being tapped you simply have to:

  1. Create a ColorStateSelector XML file
  2. Set the "backgroundTint" property of the button to the aforementioned XML file
  3. Set the button's state to the one desired and defined in the ColorState file

As an example in Kotlin and Material Buttons:

ColorStateSelector file (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>

Layout XML:

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

Kotlin file:

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!

Best of lucks!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!