简体   繁体   中英

How to remove default talk back response on particular views

So basically I am implementing talk back feature for my android app. There is Edit Text View for user to write there phone number and then there is a button for sign up. If user hasn't given any phone number the Sign Up Button is disabled. When the Button is disabled it the talk back should say "Sign Up Button Disabled, Please enter a valid mobile number." But since there is default string which talk back is saying at the end ie: Button Disabled. How to remove the default string being said for a particular view.

verifyButton.disable()
verifyButton.contentDescription = requireContext().getString(R.string.sign_up_button_disabled_phone_number_accessibility_label)

You can achieve this by attaching an AccessibilityDelegate to the view:

ViewCompat.setAccessibilityDelegate(submitButton, object: AccessibilityDelegateCompat() {
    override fun onInitializeAccessibilityNodeInfo(
        host: View,
        info: AccessibilityNodeInfoCompat
    ) {
          super.onInitializeAccessibilityNodeInfo(host, info)
          if (!submitButton.isEnabled)
              info.stateDescription = getString(R.string.accessibility_button_not_enabled_reason) //"Please enter a phone number"
      }
})

This will update every time the state of the view changes so you won't need to manually do it on validation.

This will announce:

"Please enter a phone number, [Button Text], Button, Disabled"

So the user will still be aware of the button and it's intention and additionally why it's disabled!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM