按钮-单击更改背景颜色 [英] Button - Change background color on click
问题描述
我的活动中有8个按钮。我要寻找的是,按钮具有默认背景,并且单击按钮时,背景颜色应更改为其他颜色。这部分很简单。但是,当我单击任何其他按钮时,第一个按钮的背景色应更改回默认颜色。我知道这将使用选择器状态完成,但是我不确定如何实现它。
现在,我拥有的xml如下:在drawable文件夹中
<?xml version = 1.0 encoding = utf-8?>
<选择器xmlns:android = http://schemas.android.com/apk/res/android>
<项目android:drawable = @ color / blue android:state_pressed = true />
< item android:drawable = @ color / dark_grey android:state_focused = true />
< item android:drawable = @ drawable / image_border />
< / selector>
xml中的drawable / image_border用于定义按钮的形状。以下是image_border.xml
<?xml version = 1.0 encoding = UTF-8?>
< shape xmlns:android = http://schemas.android.com/apk/res/android>
< solid android:color = @ color / dark_grey />
<描边
android:width = 4dp
android:color = @ color / light_grey />
<填充
android:bottom = 1dp
android:left = 1dp
android:right = 1dp
android: top = 1dp />
< / shape>
有人可以帮我改变xml使其表现为我需要的方式吗? / p>
以下所有答案都指向类似的解。这是我所做的更改。但是,尽管如此,当我按下按钮时,它会变为指定的颜色,但会立即恢复为默认颜色。
button_background_blue.xml
<?xml version = 1.0 encoding = utf-8?>
<选择器xmlns:android = http://schemas.android.com/apk/res/android>
<项目android:drawable = @ drawable / image_border_blue android:state_pressed = true />
< item android:drawable = @ color / dark_grey android:state_focused = true />
< item android:drawable = @ drawable / image_border />
< / selector>
image_border_blue.xml
<?xml version = 1.0 encoding = UTF-8?>
< shape xmlns:android = http://schemas.android.com/apk/res/android>
< solid android:color = @ color / blue />
<描边
android:width = 4dp
android:color = @ color / blue />
<填充
android:bottom = 1dp
android:left = 1dp
android:right = 1dp
android: top = 1dp />
< / shape>
有什么想法吗?
创建一个名为 button_pressed.xml
< shape xmlns:android = http://schemas.android.com/apk/res/android>
< solid android:color = @ color / blue />
<描边
android:width = 4dp
android:color = @ color / blue />
<填充
android:bottom = 1dp
android:left = 1dp
android:right = 1dp
android: top = 1dp />
< / shape>
假设您有两个按钮,其ID为 R.id.btn
和 R.id.btn1
< ; LinearLayout xmlns:android = http://schemas.android.com/apk/res/android
android:layout_width = match_parent
android:layout_height = match_parent
android :orientation = vertical>
<按钮
android:id = @ + id / btn
android:layout_width = wrap_content
android:layout_height = wrap_content
android:layout_margin = 12dp
android:background = @ drawable / button_pressed
android:onClick = onClick
android:text = Press Me 1 />
<按钮
android:id = @ + id / btn2
android:layout_width = wrap_content
android:layout_height = wrap_content
android:layout_margin = 12dp
android:background = @ drawable / button_pressed
android:onClick = onClick
android:text = Press Me 2 />
< / LinearLayout>
编写 onClick()
方法
按钮按钮;让您保留更改的颜色,直到按下另一个按钮为止。
public void onClick(View v){
Drawable dr = getResources()。getDrawable(R.drawable.button_pressed);
dr.setColorFilter(Color.parseColor(#FF0000),PorterDuff.Mode.SRC_ATOP);
开关(v.getId()){
case R.id.btn:
if(button == null){
button = (按钮)findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button =(Button)findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
休息时间;
case R.id.btn2:
if(button == null){
button =(Button)findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button =(Button)findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
休息时间;
默认值:
休息时间;
}
}
我认为,现在您将获得想要的东西
I have 8 buttons in my activity. What I am looking for is, The buttons have a default background and when a button is clicked, the background color should change to some other color. This part is pretty straight forward. But, when I click on any other button, the background color of the first button should change back to default color. I understand that this will be done using "selector states", but I am not quite sure of how to implement it. The more i read about it, the more confused I am.
Right now, the xml that I have is as follows: in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_pressed="true"/>
<item android:drawable="@color/dark_grey" android:state_focused="true"/>
<item android:drawable="@drawable/image_border"/>
</selector>
the drawable/image_border in the xml is used to define shape for the button. Below is the image_border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/dark_grey" />
<stroke
android:width="4dp"
android:color="@color/light_grey" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Can someone help me with how to change the xml to behave the way I need it to be?
[EDIT 1]
All the below answers are pointing towards similar kind of solution. Here are the changes that I did. But, still, when i press the button, it turns to the specified color but immediately turns back to the default color.
button_background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_border_blue" android:state_pressed="true"/>
<item android:drawable="@color/dark_grey" android:state_focused="true"/>
<item android:drawable="@drawable/image_border"/>
</selector>
image_border_blue.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/blue" />
<stroke
android:width="4dp"
android:color="@color/blue" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Any thoughts?
Create a shape named button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blue" />
<stroke
android:width="4dp"
android:color="@color/blue" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Suppose, you have two buttons whose IDs are R.id.btn
and R.id.btn1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 2" />
</LinearLayout>
Write the onClick()
method which will allow you to retain the changed color until another button is pressed.
Button button;
public void onClick(View v) {
Drawable dr = getResources().getDrawable(R.drawable.button_pressed);
dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);
switch (v.getId()) {
case R.id.btn:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
case R.id.btn2:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
default:
break;
}
}
I think, now you will get What you wanted to do.
这篇关于按钮-单击更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!