[英]How to change the background color of a clicked button in android when background tint attribute is default in button?
[英]How to change the background color of a button that wasn't clicked?
我的目标是每次单击Button
背景色会恢复为默认颜色,并且单击的Button
会更改颜色。 我希望在JAVA
做到这一点,我认为for循环是可行的方法,但是我不确定如何编辑未单击的Button
。
这是我的XML
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 1" />
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 2" />
<Button
android:id="@+id/but3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 3" />
这是我的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_blue"
android:state_pressed="true"/>
<item android:drawable="@drawable/background_blue"
android:state_focused="true"/>
<item android:drawable="@drawable/background_white"/>
</selector>
这是background_white
<solid android:color="@android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
这是background_blue
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_blue_bright" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
这是我的java代码
public class MainActivity extends AppCompatActivity {
Button but1;
Button but2;
Button but3;
Button but4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but3 = (Button) findViewById(R.id.but3);
but4 = (Button) findViewById(R.id.but4);
}
public void buttonOn(View v) {
Fragment view;
switch(v.getId()) {
case R.id.but1:
Log.i("Button 1", "pressed");
break;
case R.id.but2:
Log.i("Button 2", "pressed");
break;
case R.id.but3:
Log.i("Button 3", "pressed");
break;
case R.id.but4:
Log.i("Button 4", "pressed");
break;
}
}
将背景作为可绘制的选择器添加到按钮。还添加android:focusableInTouchMode =“ true”以使按钮在按下时可以触摸。
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 1" />
然后在background_selector内部为选中和未选中的按钮添加两个可绘制对象
background_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_blue"
android:state_pressed="true"/>
<item android:drawable="@drawable/background_blue"
android:state_focused="true"/>
<item android:drawable="@drawable/background_white"/>
</selector>
然后将background_white和background_blue添加到drawable文件夹
background_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_blue_bright" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
public void changeButtonColors(ViewGroup layout, int clickedButton_id) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof Button) {
if (v.getId() == clickedButton_id){
((Button)v).setBackgroundColor(Color.RED);
}else{
((Button)v).setBackgroundColor(Color.BLUE);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.