Android 理解View.drawableStateChanged
有人能解释一下什么时候叫电话吗?我想将其与一起使用,以使完整的Android 理解View.drawableStateChanged,android,android-layout,Android,Android Layout,有人能解释一下什么时候叫电话吗?我想将其与一起使用,以使完整的视图组“光学”对焦,这意味着当此视图组的编辑文本获得对焦时更改背景色 当我实现View.drawableStateChanged时,它经常被调用,我如何知道当前调用是我关心的调用?与设置将侦听器的焦点放在子视图上相比,它有什么优势?您可以了解更多信息 View.drawableStateChanged()在视图的状态发生此类更改时调用 一种影响显示的可提取项状态的方式 如果您创建如下可绘制图形: <?xml version="1
视图组
“光学”对焦,这意味着当此视图组
的编辑文本
获得对焦时更改背景色
当我实现
View.drawableStateChanged
时,它经常被调用,我如何知道当前调用是我关心的调用?与设置将侦听器的焦点放在子视图上相比,它有什么优势?您可以了解更多信息
View.drawableStateChanged()
在视图的状态发生此类更改时调用
一种影响显示的可提取项状态的方式
如果您创建如下可绘制图形:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/>
<item android:state_focused="true"
android:color="#ff0000ff"/>
<item android:state_enabled="true"
android:color="#ff00ffff"/>
<item android:color="#ff000000"/>
</selector>
此可绘制图形主要有不同的状态(如state\u pressed
,state\u focused
,state\u enabled
)。如果已将drawable设置为特定视图,并且该视图可单击,则当您单击该视图的某个视图时,该状态通常会更改
您可以使用setOnFocusChangeListener
检查焦点更改和setOnTouchListener
触摸事件。
此外,您还可以选中以启用/禁用该视图的状态
onClick()
事件 为了简单起见,您希望整个视图组
在其中一个子对象实际处于焦点时处于焦点状态,您只需要Viewgroup.setAddStatesFromChildren
在我的代码中创建一个可绘制的文件,我称之为查看焦点:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="19dp"/>
<stroke android:color="@color/green" android:width="2dp"/>
</shape>
</item>
<item android:state_enabled="true" android:state_focused="false">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="19dp"/>
<stroke android:color="@color/black" android:width="2dp"/>
</shape>
</item>
</selector>
这是使用上述行后的结果
在使用ViewGroup.setAddStatesFromChildren
通过阅读说明以下内容的文档:
This function is called whenever the state of the view changes in
such a way that it impacts the state of drawables being shown.
当组件需要重新绘制时,框架似乎会调用此函数,您可以覆盖它(例如)执行重新绘制组件时需要执行的特定于应用程序的逻辑,例如在组件顶部手动绘制某个对象,或者更改字体,或者做一些不可能使用stock属性的事情
有一个您如何实现它的示例。谢谢,这是我最完整的答案。赏金!下次您的样本需要白色以外的背景时;-)
relativeLayout = findViewById(R.id.parentlayout);
relativeLayout.setAddStatesFromChildren(true);
This function is called whenever the state of the view changes in
such a way that it impacts the state of drawables being shown.