我有3个按钮,图像和文字下方。我想在点击时更改按钮的背景颜色。如果我点击第一个按钮,则其颜色应变为绿色,其他按钮应为黄色。如果我点击第二个按钮,则第二个按钮应为绿色,其他两个应为黄色。
有没有办法做到这一点?
MainActivity.java
Button home = (Button) findViewById(R.id.bthomeprofile);
Button friend = (Button) findViewById(R.id.btfriendsprofile);
Button office = (Button) findViewById(R.id.btofficeprofile);
/*home.setOnClickListener(this);
friend.setOnClickListener(this);
office.setOnClickListener(this);*/
public void onClick(View v)
{
if(v.getId()== R.id.bthomeprofile )
{
isButtonClicked = !isButtonClicked;
v.setBackgroundResource(isButtonClicked ? R.drawable.circle_button : R.drawable.button_color_change);
}
else if(v.getId()== R.id.btfriendsprofile)
{
isButtonClicked = !isButtonClicked;
v.setBackgroundResource(isButtonClicked ? R.drawable.circle_button : R.drawable.button_color_change);
}
else if(v.getId()== R.id.btofficeprofile)
{
isButtonClicked = !isButtonClicked;
v.setBackgroundResource(isButtonClicked ? R.drawable.circle_button : R.drawable.button_color_change);
}
}
activity_main.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bthomeprofile"
android:layout_width="102dp"
android:layout_height="102dp"
android:background="@drawable/circle_button"
android:src="@drawable/ripple_effect"
android:drawableTop="@drawable/homeicon"
android:paddingTop="20dp"
android:text="Home"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="serif"
android:onClick="homeProfile"/>
<Button
android:id="@+id/btfriendsprofile"
android:layout_width="102dp"
android:layout_height="102dp"
android:layout_marginLeft="10dp"
android:background="@drawable/circle_button"
android:drawableTop="@drawable/friendsicon"
android:paddingTop="20dp"
android:text="Friends"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="serif"
android:onClick="friendsProfile"/>
<Button
android:id="@+id/btofficeprofile"
android:layout_width="102dp"
android:layout_height="102dp"
android:layout_marginLeft="10dp"
android:background="@drawable/circle_button"
android:drawableTop="@drawable/officeicon"
android:paddingTop="20dp"
android:text="Office"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="serif"
android:onClick="officeProfile"/>
</LinearLayout>
circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#32c24d" />
<stroke
android:width="2dip"
android:color="#c88f08" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
答案 0 :(得分:2)
只需保留previousButtonClicked
的参考Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
Button b3 = (Button) findViewById(R.id.b3);
Button previousButton = null;
将所有按钮背景设置为黄色
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//set Background color Green to b1
if(previousButton != null){
//setBackGround color Yellow to previousButton
}
previousButton = b1;
}
});
也可以在其他按钮的onClickListener中执行此操作 在你的案例中
Button home = (Button) findViewById(R.id.bthomeprofile);
Button friend = (Button) findViewById(R.id.btfriendsprofile);
Button office = (Button) findViewById(R.id.btofficeprofile);
Button previousClickedButton = null;
public void onClick(View v){
if(v.getId()== R.id.bthomeprofile){
//set color to green
//do your stuf
}
else if(v.getId()== R.id.btfriendsprofile){
//set color to green
//do your stuff
}
else if(v.getId()== R.id.btofficeprofile){
//set color to green
//do your stuff
}
if(previousClickedButton != null){
//setColor to yellow
}
previousClickedButton = (Button)v;
}
答案 1 :(得分:0)
我的解决方案是使用状态drawable并将其分配给您的3个按钮。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_sel" android:state_selected="true" />
<item android:drawable="@drawable/button_sel" android:state_pressed="true" />
<item android:drawable="@drawable/button_unsel" />
</selector>