让我澄清这个问题。我有4个图像按钮,我想更改按下的按钮的图标,然后如果我按另一个按钮,所以下一个按钮应该更改,最后一个应该返回到其上一个图标。如何在Android中动态更改按下的ImageButton的图标?
注意:该示例是Instagram。当你点击每个图标时,你会看到一些灰色图标,它会变成黑色。当你点击另一个时,最后一个变成灰色,新的变成黑色。
让我澄清这个问题。我有4个图像按钮,我想更改按下的按钮的图标,然后如果我按另一个按钮,所以下一个按钮应该更改,最后一个应该返回到其上一个图标。如何在Android中动态更改按下的ImageButton的图标?
注意:该示例是Instagram。当你点击每个图标时,你会看到一些灰色图标,它会变成黑色。当你点击另一个时,最后一个变成灰色,新的变成黑色。
您可以使用下面的代码创建在绘制文件夹中的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>
,并使用不同的图像,这取决于按下状态。
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));
}
}
我不想改变背景颜色。我想完全改变图标。有每个按钮1的2个图标是灰色的,1个是黑色的。我想要将图标1变为黑色时,点击(其余灰色),图标2变为黑色(其余灰色)等等 – Amin
因为我改变了颜色..您可以更改图标。 Like b1.setBackgroundColor(getResources()。getColor(R.color.color_gray)); –
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 !!
使用不同的可绘制了按钮的不同状态。 – SripadRaj
@SripadRaj我是新来的android编程可以给我一个例子吗? :) – Amin
你可以检查这个解决方案吗?https://stackoverflow.com/questions/32534076/what-is-the-best-way-to-do-a-button-group-that-can-be-selected- and-activate-inde – ViramP