如何在 android 中动态更改按下的 ImageButton 的图标?
How to change icon of the pressed ImageButton dynamically in android?
让我澄清一下 question.I 有 4 个图像按钮,我想更改按下的按钮的图标,然后如果我按下另一个按钮,那么下一个应该更改,最后一个应该回到它以前的图标。
注意:示例是 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));
}
}
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 !!
让我澄清一下 question.I 有 4 个图像按钮,我想更改按下的按钮的图标,然后如果我按下另一个按钮,那么下一个应该更改,最后一个应该回到它以前的图标。
注意:示例是 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));
}
}
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 !!