CompoundButton | Android Developers
source link: https://developer.android.com/reference/android/widget/CompoundButton.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- Android API Reference
- Overview
- Android Platform
- Packages
- API level
- Class Index
- Package Index
- android
- android.accessibilityservice
- android.accounts
- android.animation
- android.annotation
- android.app
- android.app.admin
- android.app.appsearch
- android.app.appsearch.exceptions
- android.app.assist
- android.app.backup
- android.app.blob
- android.app.job
- android.app.people
- android.app.role
- android.app.slice
- android.app.usage
- android.appwidget
- android.bluetooth
- android.bluetooth.le
- android.companion
- android.content
- android.content.pm
- android.content.res
- android.content.res.loader
- android.database
- android.database.sqlite
- android.drm
- android.gesture
- android.graphics
- android.graphics.drawable
- android.graphics.drawable.shapes
- android.graphics.fonts
- android.graphics.pdf
- android.graphics.text
- android.hardware
- android.hardware.biometrics
- android.hardware.camera2
- android.hardware.camera2.params
- android.hardware.display
- android.hardware.fingerprint
- android.hardware.input
- android.hardware.usb
- android.icu.lang
- android.icu.math
- android.icu.number
- android.icu.text
- android.icu.util
- android.inputmethodservice
- android.location
- android.location.provider
- android.media
- android.media.audiofx
- android.media.browse
- android.media.effect
- android.media.metrics
- android.media.midi
- android.media.projection
- android.media.session
- android.media.tv
- android.mtp
- android.net
- android.net.eap
- android.net.http
- android.net.ipsec.ike
- android.net.ipsec.ike.exceptions
- android.net.nsd
- android.net.rtp
- android.net.sip
- android.net.ssl
- android.net.vcn
- android.net.wifi
- android.net.wifi.aware
- android.net.wifi.hotspot2
- android.net.wifi.hotspot2.omadm
- android.net.wifi.hotspot2.pps
- android.net.wifi.p2p
- android.net.wifi.p2p.nsd
- android.net.wifi.rtt
- android.nfc
- android.nfc.cardemulation
- android.nfc.tech
- android.opengl
- android.os
- android.os.health
- android.os.storage
- android.os.strictmode
- android.preference
- android.print
- android.print.pdf
- android.printservice
- android.provider
- android.renderscript
- android.sax
- android.se.omapi
- android.security
- android.security.identity
- android.security.keystore
- android.service.autofill
- android.service.carrier
- android.service.chooser
- android.service.controls
- android.service.controls.actions
- android.service.controls.templates
- android.service.dreams
- android.service.media
- android.service.notification
- android.service.quickaccesswallet
- android.service.quicksettings
- android.service.restrictions
- android.service.textservice
- android.service.voice
- android.service.vr
- android.service.wallpaper
- android.speech
- android.speech.tts
- android.system
- android.telecom
- android.telephony
- android.telephony.cdma
- android.telephony.data
- android.telephony.emergency
- android.telephony.euicc
- android.telephony.gsm
- android.telephony.ims
- android.telephony.ims.feature
- android.telephony.mbms
- android.test
- android.test.mock
- android.test.suitebuilder
- android.test.suitebuilder.annotation
- android.text
- android.text.format
- android.text.method
- android.text.style
- android.text.util
- android.transition
- android.util
- android.util.proto
- android.view
- android.view.accessibility
- android.view.animation
- android.view.autofill
- android.view.contentcapture
- android.view.inputmethod
- android.view.inspector
- android.view.textclassifier
- android.view.textservice
- android.webkit
- android.widget
- android.widget.inline
- android.window
- com.android.net.module.util
- com.google.android.collect
- com.google.android.gles_jni
- com.google.android.util
- dalvik.annotation
- dalvik.bytecode
- dalvik.system
- java.awt.font
- java.beans
- java.io
- java.lang
- java.lang.annotation
- java.lang.invoke
- java.lang.ref
- java.lang.reflect
- java.math
- java.net
- java.nio
- java.nio.channels
- java.nio.channels.spi
- java.nio.charset
- java.nio.charset.spi
- java.nio.file
- java.nio.file.attribute
- java.nio.file.spi
- java.security
- java.security.acl
- java.security.cert
- java.security.interfaces
- java.security.spec
- java.sql
- java.text
- java.time
- java.time.chrono
- java.time.format
- java.time.temporal
- java.time.zone
- java.util
- java.util.concurrent
- java.util.concurrent.atomic
- java.util.concurrent.locks
- java.util.function
- java.util.jar
- java.util.logging
- java.util.prefs
- java.util.regex
- java.util.stream
- java.util.zip
- javax.crypto
- javax.crypto.interfaces
- javax.crypto.spec
- javax.microedition.khronos.egl
- javax.microedition.khronos.opengles
- javax.net
- javax.net.ssl
- javax.security.auth
- javax.security.auth.callback
- javax.security.auth.login
- javax.security.auth.x500
- javax.security.cert
- javax.sql
- javax.xml
- javax.xml.datatype
- javax.xml.namespace
- javax.xml.parsers
- javax.xml.transform
- javax.xml.transform.dom
- javax.xml.transform.sax
- javax.xml.transform.stream
- javax.xml.validation
- javax.xml.xpath
- junit.framework
- junit.runner
- org.apache.http.conn
- org.apache.http.conn.scheme
- org.apache.http.conn.ssl
- org.apache.http.params
- org.json
- org.w3c.dom
- org.w3c.dom.ls
- org.xml.sax
- org.xml.sax.ext
- org.xml.sax.helpers
- org.xmlpull.v1
- org.xmlpull.v1.sax2
Android 12 Developer Preview is here! Try it out, and give us your feedback!
CompoundButton
public
abstract
class
CompoundButton
extends Button
implements
Checkable
A button with two states, checked and unchecked. When the button is pressed or clicked, the state changes automatically.
XML attributes
See CompoundButton Attributes
, Button
Attributes
, TextView Attributes
, View Attributes
Summary
Nested classes
interface
CompoundButton.OnCheckedChangeListener
Interface definition for a callback to be invoked when the checked state of a compound button changed.
XML attributes
android:button
Drawable used for the button graphic (for example, checkbox and radio button).
android:buttonTint
Tint to apply to the button graphic.
android:buttonTintMode
Blending mode used to apply the button graphic tint.
Inherited fields
From classandroid.view.View
Public methods
void
autofill(AutofillValue value)
Automatically fills the content of this view with the value
.
void
drawableHotspotChanged(float x, float y)
This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.
CharSequence
getAccessibilityClassName()
Return the class name of this object to be used for accessibility purposes.
int
getAutofillType()
Describes the autofill type of this view, so an
AutofillService
can create the proper AutofillValue
when autofilling the view.
AutofillValue
getAutofillValue()
Gets the TextView
's current text for AutoFill.
Drawable
getButtonDrawable()
BlendMode
getButtonTintBlendMode()
ColorStateList
getButtonTintList()
PorterDuff.Mode
getButtonTintMode()
int
getCompoundPaddingLeft()
Returns the left padding of the view, plus space for the left Drawable if any.
int
getCompoundPaddingRight()
Returns the right padding of the view, plus space for the right Drawable if any.
boolean
isChecked()
void
jumpDrawablesToCurrentState()
Call Drawable.jumpToCurrentState()
on all Drawable objects associated with this view.
void
onRestoreInstanceState(Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously
been generated by onSaveInstanceState()
.
Parcelable
onSaveInstanceState()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.
boolean
performClick()
Call this view's OnClickListener, if it is defined.
void
setButtonDrawable(int resId)
Sets a drawable as the compound button image given its resource identifier.
void
setButtonDrawable(Drawable drawable)
Sets a drawable as the compound button image.
void
setButtonIcon(Icon icon)
Sets the button of this CompoundButton to the specified Icon.
void
setButtonTintBlendMode(BlendMode tintMode)
Specifies the blending mode used to apply the tint specified by
setButtonTintList(android.content.res.ColorStateList)
} to the button drawable.
void
setButtonTintList(ColorStateList tint)
Applies a tint to the button drawable.
void
setButtonTintMode(PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by
setButtonTintList(android.content.res.ColorStateList)
} to the button drawable.
void
setChecked(boolean checked)
Changes the checked state of this button.
void
setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)
Register a callback to be invoked when the checked state of this button changes.
void
setStateDescription(CharSequence stateDescription)
This function is called when an instance or subclass sets the state description.
void
toggle()
Change the checked state of the view to the inverse of its current state
Protected methods
void
drawableStateChanged()
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
int[]
onCreateDrawableState(int extraSpace)
Generate the new Drawable
state for
this view.
void
onDraw(Canvas canvas)
Implement this to do your drawing.
boolean
verifyDrawable(Drawable who)
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.
XML attributes
android:button
Drawable used for the button graphic (for example, checkbox and radio button).
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
Related methods:
android:buttonTint
Tint to apply to the button graphic.
May be a color value, in the form of "#rgb
",
"#argb
", "#rrggbb
", or
"#aarrggbb
".
Related methods:
android:buttonTintMode
Blending mode used to apply the button graphic tint.
Must be one of the following constant values.
Related methods:
Public constructors
CompoundButton
public CompoundButton (Context context, AttributeSet attrs)
context
Context
attrs
AttributeSet
CompoundButton
public CompoundButton (Context context, AttributeSet attrs, int defStyleAttr)
context
Context
attrs
AttributeSet
defStyleAttr
int
CompoundButton
public CompoundButton (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
context
Context
attrs
AttributeSet
defStyleAttr
int
defStyleRes
int
Public methods
autofill
public void autofill (AutofillValue value)
Automatically fills the content of this view with the value
.
Views support the Autofill Framework mainly by:
- Providing the metadata defining what the view means and how it can be autofilled.
- Implementing the methods that autofill the view.
onProvideAutofillStructure(android.view.ViewStructure, int)
is responsible for the former,
this method is responsible for latter.
This method does nothing by default, but when overridden it typically:
- Checks if the provided value matches the expected type (which is defined by
getAutofillType()
). - Checks if the view is editable - if it isn't, it should return right away.
- Call the proper getter method on
AutofillValue
to fetch the actual value. - Pass the actual value to the equivalent setter in the view.
For example, a text-field view could implement the method this way:
@Override
public void autofill(AutofillValue value) {
if (!value.isText() || !this.isEditable()) {
return;
}
CharSequence text = value.getTextValue();
if (text != null) {
this.setText(text);
}
}
If the value is updated asynchronously, the next call to
AutofillManager#notifyValueChanged(View)
must happen after the value was
changed to the autofilled value. If not, the view will not be considered autofilled.
Note: After this method is called, the value returned by
getAutofillValue()
must be equal to the value
passed to it, otherwise the
view will not be highlighted as autofilled.
value
AutofillValue
: value to be autofilled.
drawableHotspotChanged
public void drawableHotspotChanged (float x, float y)
This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.
Dispatching to child views is handled by
dispatchDrawableHotspotChanged(float, float)
.
Be sure to call through to the superclass when overriding this function.
If you override this method you must call through to the
superclass implementation.
x
float
: hotspot x coordinate
y
float
: hotspot y coordinate
getAccessibilityClassName
public CharSequence getAccessibilityClassName ()
Return the class name of this object to be used for accessibility purposes.
Subclasses should only override this if they are implementing something that
should be seen as a completely new class of view when used by accessibility,
unrelated to the class it is deriving from. This is used to fill in
AccessibilityNodeInfo.setClassName
.
CharSequence
getAutofillType
public int getAutofillType ()
Describes the autofill type of this view, so an
AutofillService
can create the proper AutofillValue
when autofilling the view.
By default returns AUTOFILL_TYPE_NONE
, but views should override it to properly
support the Autofill Framework.
getAutofillValue
public AutofillValue getAutofillValue ()
Gets the TextView
's current text for AutoFill. The value is trimmed to 100K
char
s if longer.
AutofillValue
current text, null
if the text is not editable
getButtonDrawable
public Drawable getButtonDrawable ()
Drawable
the drawable used as the compound button image
This value may be null
.
getButtonTintBlendMode
public BlendMode getButtonTintBlendMode ()
Related XML Attributes:
BlendMode
the blending mode used to apply the tint to the button drawable
This value may be null
.
See also:
getButtonTintList
public ColorStateList getButtonTintList ()
Related XML Attributes:
ColorStateList
the tint applied to the button drawable
This value may be null
.
See also:
getButtonTintMode
public PorterDuff.Mode getButtonTintMode ()
Related XML Attributes:
PorterDuff.Mode
the blending mode used to apply the tint to the button drawable
This value may be null
.
See also:
getCompoundPaddingLeft
public int getCompoundPaddingLeft ()
Returns the left padding of the view, plus space for the left Drawable if any.
int
getCompoundPaddingRight
public int getCompoundPaddingRight ()
Returns the right padding of the view, plus space for the right Drawable if any.
int
isChecked
public boolean isChecked ()
boolean
jumpDrawablesToCurrentState
public void jumpDrawablesToCurrentState ()
Call Drawable.jumpToCurrentState()
on all Drawable objects associated with this view.
Also calls StateListAnimator#jumpToCurrentState()
if there is a StateListAnimator
attached to this view.
If you override this method you must call through to the
superclass implementation.
onRestoreInstanceState
public void onRestoreInstanceState (Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously
been generated by onSaveInstanceState()
. This function will never be called with a
null state.
If you override this method you must call through to the
superclass implementation.
state
Parcelable
: The frozen state that had previously been returned by
onSaveInstanceState()
.
onSaveInstanceState
public Parcelable onSaveInstanceState ()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.
Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.
If you override this method you must call through to the
superclass implementation.
Parcelable
Returns a Parcelable object containing the view's current dynamic
state, or null if there is nothing interesting to save.
performClick
public boolean performClick ()
Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
boolean
True there was an assigned OnClickListener that was called, false
otherwise is returned.
setButtonDrawable
public void setButtonDrawable (int resId)
Sets a drawable as the compound button image given its resource identifier.
Related XML Attributes:
resId
int
: the resource identifier of the drawable
setButtonDrawable
public void setButtonDrawable (Drawable drawable)
Sets a drawable as the compound button image.
Related XML Attributes:
drawable
Drawable
: the drawable to set
This value may be null
.
setButtonIcon
public void setButtonIcon (Icon icon)
Sets the button of this CompoundButton to the specified Icon.
icon
Icon
: an Icon holding the desired button, or null
to clear
the button
This value may be null
.
setButtonTintBlendMode
public void setButtonTintBlendMode (BlendMode tintMode)
Specifies the blending mode used to apply the tint specified by
setButtonTintList(android.content.res.ColorStateList)
} to the button drawable. The
default mode is PorterDuff.Mode#SRC_IN
.
Related XML Attributes:
tintMode
BlendMode
: the blending mode used to apply the tint, may be
null
to clear tint
This value may be null
.
setButtonTintList
public void setButtonTintList (ColorStateList tint)
Applies a tint to the button drawable. Does not modify the current tint
mode, which is PorterDuff.Mode#SRC_IN
by default.
Subsequent calls to setButtonDrawable(android.graphics.drawable.Drawable)
will
automatically mutate the drawable and apply the specified tint and tint
mode using
Drawable#setTintList(ColorStateList)
.
Related XML Attributes:
Parameterstint
ColorStateList
: the tint to apply, may be null
to clear tint
This value may be null
.
setButtonTintMode
public void setButtonTintMode (PorterDuff.Mode tintMode)
Specifies the blending mode used to apply the tint specified by
setButtonTintList(android.content.res.ColorStateList)
} to the button drawable. The
default mode is PorterDuff.Mode#SRC_IN
.
Related XML Attributes:
tintMode
PorterDuff.Mode
: the blending mode used to apply the tint, may be
null
to clear tint
This value may be null
.
setChecked
public void setChecked (boolean checked)
Changes the checked state of this button.
checked
boolean
: true to check the button, false to uncheck it
setOnCheckedChangeListener
public void setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)
Register a callback to be invoked when the checked state of this button changes.
listener
CompoundButton.OnCheckedChangeListener
: the callback to call on checked state change
This value may be null
.
setStateDescription
public void setStateDescription (CharSequence stateDescription)
This function is called when an instance or subclass sets the state description. Once this
is called and the argument is not null, the app developer will be responsible for updating
state description when checked state changes and we will not set state description
in setChecked(boolean)
. App developers can restore the default behavior by setting the
argument to null. If setChecked(boolean)
is called first and then setStateDescription is
called, two state change events will be merged by event throttling and we can still get
the correct state description.
stateDescription
CharSequence
: The state description.
This value may be null
.
toggle
public void toggle ()
Change the checked state of the view to the inverse of its current state
Protected methods
drawableStateChanged
protected void drawableStateChanged ()
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
If the View has a StateListAnimator, it will also be called to run necessary state change animations.
Be sure to call through to the superclass when overriding this function.
If you override this method you must call through to the
superclass implementation.
onCreateDrawableState
protected int[] onCreateDrawableState (int extraSpace)
Generate the new Drawable
state for
this view. This is called by the view
system when the cached Drawable state is determined to be invalid. To
retrieve the current state, you should use getDrawableState()
.
extraSpace
int
: if non-zero, this is the number of extra entries you
would like in the returned array in which you can place your own
states.
int[]
Returns an array holding the current Drawable
state of
the view.
onDraw
protected void onDraw (Canvas canvas)
Implement this to do your drawing.
canvas
Canvas
: the canvas on which the background will be drawn
verifyDrawable
protected boolean verifyDrawable (Drawable who)
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.
Be sure to call through to the super class when overriding this function.
If you override this method you must call through to the
superclass implementation.
who
Drawable
: This value cannot be null
.
boolean
boolean If true then the Drawable is being displayed in the
view; else false and it is not allowed to animate.
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2021-03-17 UTC.
-
Follow @AndroidDev on Twitter
-
Check out Android Developers on YouTube
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK