Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageButton onClick更改颜色_Android_Imagebutton - Fatal编程技术网

Android ImageButton onClick更改颜色

Android ImageButton onClick更改颜色,android,imagebutton,Android,Imagebutton,我正在创建一个ImageButton,如下所示: <ImageButton android:id="@+id/one" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="0.333" an

我正在创建一个ImageButton,如下所示:

          <ImageButton
                android:id="@+id/one"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.333"
                android:adjustViewBounds="true"
                android:background="@null"
                android:contentDescription="@string/description_image_button_one"
                android:scaleType="fitEnd"
                android:src="@drawable/dialpad_1" />


我想实现一种方法,让按钮在点击/按下时闪烁或改变颜色,以确定它是否被点击。我知道我可以将背景作为一个可绘制的参考,并将状态选择器按到特定的颜色。我试图避免为drawable中的每个按钮创建单独的xml文件。在不创建所有这些额外的xml文件的情况下,最好的方法是什么?

您试图避免的xml文件是实现所需内容的简单方法。
另一种方法是使用
onClickListener
中的代码进行操作,我认为这是一项需要做更多工作的任务。

您应该使用XML文件,否则什么都不做。Android为按下的按钮提供默认颜色。您需要为按钮背景创建自定义可绘制选择器

此文件将保存在您的XML文件夹中,看起来像这样(每个元素描述处于不同选定状态时的按钮背景):

文件名为:res/drawable/my_custom_selector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/blue_button_on"
        android:state_focused="true"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/blue_button_on"
        android:state_focused="false"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/blue_button_off"
        android:state_focused="true"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/blue_button_off"
        android:state_focused="false"
        android:state_pressed="false"/>
</selector>

然后,要将该背景应用于ImageView(或任何视图),只需将其设置为背景:

 <ImageButton
                android:id="@+id/one"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="0.333"
                android:adjustViewBounds="true"
                android:background="@drawable/my_custom_selector"
                android:contentDescription="@string/description_image_button_one"
                android:scaleType="fitEnd"
                android:src="@drawable/dialpad_1" />


在这种情况下,不要设置任何背景色,它将正常工作。或者单击按钮的“更改颜色”。不设置背景色是什么意思?我现在有@null。意味着没有背景颜色,android在点击任何按钮时提供默认颜色。。你会觉得你有压力或使用选择器,因为每个按钮都有不同的src,所以我必须为每个按钮创建一个单独的xml,对吗?事实上,但另一种选择是,你必须使用代码为按下和未按下的状态指定不同的纹理,这更糟糕。所以我认为你应该这样做。我没有回答你的问题吗?是的,我正在尝试实现另一个答案,看看它是否有效。另一个答案是什么?Booger建议的选择器?明白了。因此,如果我有12个按钮,我需要创建12个xml文件?不,相同的背景可以用于所有按钮。也就是说,只需将相同的背景应用于每个按钮(按下时,它们的外观都相同)。按钮1.退刀台(我的可牵引);按钮2.退刀台(my_可牵引);等