Android:更改背景颜色 View onClick Button

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22682767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:20:18  来源:igfitidea点击:

Android: change background color View onClick Button

androidviewcolorsbackground

提问by Charlie

How I can change the background color to a Viewthis under a button when I click the button? I've tried a selectordoes not work because the View not change the color. What is the problem?

View单击button?时,如何将按钮下的背景颜色更改为this ?我试过一个selector不起作用,因为视图不会改变颜色。问题是什么?

This is my code:

这是我的代码:

XML

XML

    ...
          <View
            android:id="@+id/viewPlanos2"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_gravity="center" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@color/transparente"
            android:drawableTop="@drawable/buttonimage"
            android:gravity="center"
            android:paddingTop="50dp" />

        <View
            android:id="@+id/viewPlanos1"
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_gravity="center" />
 ...

JAVA

爪哇

View linea2 = (View)findViewById(R.id.viewPlanos2);
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));

linea_verde

linea_verde

<item android:state_pressed="true"><shape>
        <gradient android:angle="90" android:endColor="@color/azul" android:startColor="@color/azulOscuro" />
    </shape></item>
<item android:state_focused="true"><shape>
        <gradient android:angle="90" android:endColor="@color/azul" android:startColor="@color/azulOscuro" />
    </shape></item>
<item><shape>
        <solid android:color="@color/rojo" />
    </shape></item>

EDIT:

编辑:

I have tried:

我试过了:

public void onClick(View v) {

    if(v == botonMetro) {

        linea2.setBackgroundResource(R.drawable.linea_verde);
                    and

        linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
    }
}

But the code not work

但代码不起作用

回答by Naveen Kumar

You are wrongly using drawable use this one one

你错误地使用 drawable 使用这个一个

 view.setBackgroundResource(R.drawable.linea_verde)

回答by MilapTank

as you say change the background color to a View this under a button when I click the button

正如你所说,当我单击按钮时,将背景颜色更改为在按钮下查看

do like this

这样做

 button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

                  // change color of your view here  
           linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));

          }
    });

回答by FD_

setBackgroundColor()is for colors only, but it seems your using a state list drawable. If you are sure you want to use a different color depending on the Button's state, set the state list drawable using this code:

setBackgroundColor()仅用于颜色,但似乎您使用的是可绘制的状态列表。如果您确定要根据Button的状态使用不同的颜色,请使用以下代码设置可绘制的状态列表:

view.setBackgroundDrawable(R.drawable.linea_verde);

Otherwise, just set the background color using

否则,只需使用设置背景颜色

view.setBackgroundColor(getResources().getColor(R.drawable.yourcolor);

But in this case, use a click listener, and also make sure to actually use a color resource, e.g.:

但在这种情况下,请使用点击侦听器,并确保实际使用颜色资源,例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="opaque_red">#f00</color>
    <color name="translucent_red">#80ff0000</color>
</resources>

回答by Bhavik Kamdar

You can also do like this.

你也可以这样做。

android:background="@drawable/linea_verde"

回答by Rafal Enden

If you would like to apply default background of selected item you could use backgroundattribute with android ?attr.

如果您想应用所选项目的默认背景,您可以将background属性与 android 一起使用?attr

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="?attr/selectableItemBackground" />

selectableItemBackgroundattribute will use proper drawable according to Android version. On pre-lollipop it will use solid background, but on lollipop devices the ripple effect will be used.

selectableItemBackground属性将根据 Android 版本使用适当的 drawable。在棒棒糖之前,它将使用纯色背景,但在棒棒糖设备上,将使用涟漪效应。

Code from: com.android.support/appcompat-v7/23.0.0/res/values/values.xml

代码来自:com.android.support/appcompat-v7/23.0.0/res/values/values.xml

<item name="selectableItemBackground">@drawable/abc_item_background_holo_dark</item>

回答by VayuDev

public void onClick(View v) {
    int id = v.getId();
    if(id == R.id.button) {
        linea2.setBackgroundResource(R.drawable.linea_verde);
    }
}

I think you were comparing the view the wrong way. I just happened to stumble to this question and since it is not marked as answered, this might help others.

我认为您以错误的方式比较视图。我碰巧偶然发现了这个问题,并且由于它没有被标记为已回答,这可能对其他人有所帮助。

回答by Skullper

Create two separate files for your shapes and change the background of your view after button click:

为您的形状创建两个单独的文件,并在单击按钮后更改视图的背景:

private boolean isPressed = false;    

public void onClick(View v) {
    if(v == botonMetro) {
        if(isPressed){
           linea2.setBackgroundResource(R.drawable.pressed_shape);       
        } else{
           linea2.setBackgroundResource(R.drawable.un_pressed_shape);       
        }
        isPressed = !isPressed;
    }
}