选择器继承
有没有办法从 Android 中已知的选择器继承?
我想扩展 EditText
并添加自定义状态,到目前为止我了解使用 onCreateDrawableState()
方法来实现它。当选择器发挥作用时,是否有一种简单的方法可以使用默认选择器并只需添加我的选择器而不是再次定义它们?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.my.package">
<item android:state_enabled="false" android:drawable="@drawable/login_textfield_bg_error" />
<item android:state_window_focused="false" android:drawable="@drawable/login_textfield_bg_error">
<item android:state_pressed="true" android:drawable="@drawable/login_textfield_bg_error" />
<item android:state_selected="true" android:drawable="@drawable/login_textfield_bg_error" />
<item app:errorBackground="@drawable/login_textfield_bg_error" />
</selector>
Is there a way to inherit from already known selector
s in Android ?
I would like to extend an EditText
and added an custom state, so far i understood doing it using the onCreateDrawableState()
Method. When an selector comes into play is there an easy way to use the default selectors and just add mine instead of defining them again ?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.my.package">
<item android:state_enabled="false" android:drawable="@drawable/login_textfield_bg_error" />
<item android:state_window_focused="false" android:drawable="@drawable/login_textfield_bg_error">
<item android:state_pressed="true" android:drawable="@drawable/login_textfield_bg_error" />
<item android:state_selected="true" android:drawable="@drawable/login_textfield_bg_error" />
<item app:errorBackground="@drawable/login_textfield_bg_error" />
</selector>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会误解,但也许你可以直接委托给他们?
因此,在您的情况下,您有一个自定义状态,因此,如果您仅定义自定义状态适用的情况,您不能这样做:
所以这基本上说明,对于我的自定义状态为 true 的状态,显示我的自定义背景...然而,对于所有其他状态,其行为与此选择器相同。该选择器恰好有其他状态的说明,因此也请遵循它们。因此,无需重新定义,并且由于状态是按从上到下的顺序评估的,因此从技术上讲,您不必重新定义任何内容,您只是声明您只想定义状态的子集并委托给其他可绘制对象(这恰好是是所有其他内容的另一个选择器)。这有帮助吗?
I might be misunderstanding but maybe you can just plain delegate to them?
So in your case you have a custom state, so if you define only the cases where your custom state applies can't you do this:
So this basically states, for states where my custom state is true, show my custom background... however for all other states, behave the same as this selector. This selector just so happens to have instructions for other states so follow them too. So there is no redefining and since the states are evaluated in top to bottom order, you technically don't have to redefine anything, you are just stating you only want to define a subset of states and delegate to this other drawable (which happens to be another selector) for all other content. Does this help?