让我澄清一下这个问题。我有4个图像按钮,我想更改按下的按钮的图标然后如果我按下另一个按钮,那么下一个按钮应该被更改,最后一个应该回到它之前的图标。
注意:示例是Instagram。当您单击每个图标时会看到一些灰色图标,它会变为黑色。当你点击另一个时,最后一个变为灰色,新的变为黑色。
答案 0 :(得分:1)
您可以使用以下代码在drawable文件夹中创建xml文件,并将其用作可绘制的图像:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="@drawable/button_normal_green" />
<item android:drawable="@drawable/selected_icon" android:state_selected="true"/>
</selector>
并根据印刷机状态使用不同的图像。
答案 1 :(得分:1)
public class CamTestActivity extends Activity implements View.OnClickListener{
Button b1,b2,b3,b4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.btn1);
b2 = (Button)findViewById(R.id.btn2);
b3 = (Button)findViewById(R.id.btn3);
b4 = (Button)findViewById(R.id.btn4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.btn1:
pressbtn1();
break;
case R.id.btn2:
pressbtn2();
break;
case R.id.btn3:
pressbtn3();
break;
case R.id.btn4:
pressbtn4();
break;
}
}
private void pressbtn4() {
b1.setBackgroundColor(getResources().getColor(R.color.color_gray));
b2.setBackgroundColor(getResources().getColor(R.color.color_gray));
b3.setBackgroundColor(getResources().getColor(R.color.color_gray));
b4.setBackgroundColor(getResources().getColor(R.color.color_black));
}
private void pressbtn3() {
b1.setBackgroundColor(getResources().getColor(R.color.color_gray));
b2.setBackgroundColor(getResources().getColor(R.color.color_gray));
b3.setBackgroundColor(getResources().getColor(R.color.color_black));
b4.setBackgroundColor(getResources().getColor(R.color.color_gray));
}
private void pressbtn2() {
b1.setBackgroundColor(getResources().getColor(R.color.color_gray));
b2.setBackgroundColor(getResources().getColor(R.color.color_black));
b3.setBackgroundColor(getResources().getColor(R.color.color_gray));
b4.setBackgroundColor(getResources().getColor(R.color.color_gray));
}
private void pressbtn1() {
b1.setBackgroundColor(getResources().getColor(R.color.color_black));
b2.setBackgroundColor(getResources().getColor(R.color.color_gray));
b3.setBackgroundColor(getResources().getColor(R.color.color_gray));
b4.setBackgroundColor(getResources().getColor(R.color.color_gray));
}
}
答案 2 :(得分:1)
Java代码:
public void onAnyButtonCLick(View view)
{
for (int i = 0; i < parent_layout.getChildCount(); i++) {
View v = parent_layout.getChildAt(i);
// Check v is button
if(v instanceof Button)
{
// set all button with grey color background
v.setBackgroundColor(getResources().getColor(R.color.color_gray));
}
} // loop ends
// color the current click button with black.
view.setBackgroundColor(getResources().getColor(R.color.color_black));
} // end of click
<强> XML:强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_layout"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Button"
android:onClick="button_click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Button"
android:onClick="button_click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Button"
android:onClick="button_click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forth Button"
android:onClick="button_click"/>
</LinearLayout>
// Hope it Helps !!