我的目标是每次点击一个按钮,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;
}
}
答案 0 :(得分:1)
将背景作为drawable选择器添加到button.Also add android:focusableInTouchMode =&#34; true&#34;按下按钮时按下按钮。
<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
中选中和未选中的按钮添加两个drawable<强> 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>
答案 1 :(得分:0)
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);
}
}
}
}