0

我希望 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 个按钮播放、声音和音乐组成

4

3 回答 3

0

而不是设置背景

img1.setBackground(getResources().getDrawable(R.drawable.sound_on));

你需要 setImageResource

img1.setImageResource(R.drawable.sound_on);

此外,您的 v.getId() 将始终返回相同的 ID,因此您需要另一个标志来跟踪

img1.setOnClickListener(new OnClickListener()
{
    private boolean toggle=false;
    @Override
    public void onClick(View v)
    {
        if(toggle)
        {
             img1.setBackground(getResources().getDrawable(R.drawable.sound_on));
             toggle=false;
        }
        else 
        {
             img1.setBackground(getResources().getDrawable(R.drawable.sound_off));
             toggle=true;
        }
    }
}
于 2013-11-06T12:07:24.390 回答
0

您的 if-else 无效。做这样的事情:

private boolean buttonClick = true;

@Override
public void onClick(View v)
{
    if(v.getId() == R.id.soundtoggle)
    {
       if(buttonClick){
           img1.setBackground(getResources().getDrawable(R.drawable.sound_on));
       } else{
           img1.setBackground(getResources().getDrawable(R.drawable.sound_off));
       }
       buttonClick = !buttonClick;    
    }
}

每次单击图像时,您都会看到“sound_on”图像。因为你在图片点击上设置了

于 2013-11-06T12:09:23.663 回答
0

用这个 star3.setImageResource(R.drawable.unclickstar);

代替 img1.setBackground(getResources().getDrawable(R.drawable.sound_off));

于 2013-11-06T12:13:58.993 回答