我在背景中有一个带有图像的片段,我想使edittext的边框在按下和不按下时改变颜色,但是不起作用,我尝试删除backgroung图像,我得到的结果与此相同码 rounded_edittext:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/focused"
/> <!-- focused -->
<item
android:state_pressed="false"
android:drawable="@drawable/unfocused"
/> <!-- defualt -->
</selector>
没有重点:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000" />
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</selector>
重点:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#fff" />
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
,我在这里将其添加到edittext:
android:background="@drawable/rounded_edittext"
我该如何解决?
答案 0 :(得分:0)
请尝试使用android:state_focused代替android:state_pressed。
<item
android:state_focused="true"
android:drawable="@drawable/unfocused"
/> <!-- focused -->
<item
android:state_focused="false"
android:drawable="@drawable/focused"
/>
</selector>
答案 1 :(得分:0)
问题出在这里,因为我使用的是选择器而不是形状:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000" />
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</selector>