gpt4 book ai didi

android - EditText 背景不工作

转载 作者:行者123 更新时间:2023-11-29 21:38:18 24 4
gpt4 key购买 nike

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />

<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="@drawable/black_rounded_borders" />

</RelativeLayout>

I added the useless LinearLayout just to avoid initial focus on the AutoCompleteTextView when the Activity is launched

drawable/black_rounded_borders.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#FFFFFFFF" />

<stroke
android:width="2dp"
android:color="@color/selector_black_border" />


<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />

</shape>

color/selector_black_border.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" android:color="#FF269ff5"/>
<item android:state_pressed="true" android:color="#FF269ff5"/>
<item android:state_selected="true" android:color="#FF269ff5"/>
<item android:state_focused="false" android:color="#FF000000"/>
<item android:state_pressed="false" android:color="#FF000000"/>
<item android:state_selected="false" android:color="#FF000000"/>

</selector>

Activity

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
Log.d("hasFocus", autoComplete.hasFocus() + "");
Log.d("isSelected", autoComplete.isSelected() + "");
Log.d("isPressed", autoComplete.isPressed() + "");
//All the above are false
}

Though all the states related the blue(#FF269ff5) color are false. I still get the border in blue color. Also if I change the order of states in the color/selector_black_border.xml file, like all false first and then trues, then I get the black border. Its like only the first color works.

我错过了什么?

谢谢。

最佳答案

我不能把它放入评论中,因为它太大了,但我找到了对此的解释 here ,其中指出:

A ColorStateList不能作为 <solid> 的属性传递在 XML 定义中,或者实际上是 <shape> 的任何属性.此属性作为颜色资源从 XML 中扩展出来,然后传递给 Drawable 的 setColor()。方法,它只接受一个 ARGB 值。

只有一种 Drawable 实例被设计为根据状态包含和呈现多个项目,那就是 StateListDrawable ,这就是给 <selector> 充气时得到的结果.所有其他 Drawable 实例都只是该集合的成员或独立绘制。

另请注意,膨胀的 <shape>项目实际上是 GradientDrawable而不是 ShapeDrawable .如果您查看 inflate() GradientDrawable的方法| in the source ,您可以获得有关如何使用每个属性的所有详细信息。

鸣谢@Devunwired。

但是,在此基础上,您可以创建不同的形状(每次都是矩形),每个形状都具有以下属性。在 drawable/black_rounded_borders.xml 中使用以下代码看看它是否按预期工作:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />

<stroke
android:width="2dp"
android:color="#FF269ff5" />

<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />

<stroke
android:width="2dp"
android:color="#FF269ff5" />

<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />

<stroke
android:width="2dp"
android:color="#FF269ff5" />

<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />

<stroke
android:width="1dp"
android:color="#FF000000" />

<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>
</item>
</selector>
</item>

</layer-list>

很抱歉发了这么长的帖子,而且我不知道它是否有效,但我找不到另一种发布方式。

关于android - EditText 背景不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17788771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com