我希望 Id = soundtoggle 的图像视图在 if 条件下切换到 R.drawable.sound_on,否则切换到 R.drawable.sound_off。但我做不到。请帮忙
package com.twodwarfs.fallanimation;
import com.tmp.animation.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ToggleActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
final ImageView img1 = (ImageView) findViewById(R.id.soundtoggle);
img1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if(v.getId() == R.id.soundtoggle)
{
img1.setBackground(getResources().getDrawable(R.drawable.sound_on));
}
else
{
img1.setBackground(getResources().getDrawable(R.drawable.sound_off));
}
}});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="@drawable/home_screen">
<ImageView
android:id="@+id/musictoggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/soundtoggle"
android:layout_centerHorizontal="true"
android:src="@drawable/music_off"
android:layout_marginLeft="5dp"
android:clickable="true" />
<ImageView
android:id="@+id/soundtoggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/musictoggle"
android:layout_below="@+id/playbutton"
android:src="@drawable/sound_off"
android:layout_marginLeft="1dp"
/>
<Button
android:id="@+id/playbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/soundtoggle"
android:layout_alignParentTop="true"
android:layout_marginTop="178dp"
android:background="@drawable/play"
android:layout_marginLeft="1dp" />
我的完整 xml 由 3 个按钮播放、声音和音乐组成