单击时更改图像视图

时间:2013-11-06 12:03:59

标签: android

我希望带有Id = soundtoggle的imageview在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个按钮播放,声音和音乐

3 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分: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”图像。因为您在图像上设置了

答案 2 :(得分:0)

使用它  star3.setImageResource(R.drawable.unclickstar);

而不是 img1.setBackground(getResources().getDrawable(R.drawable.sound_off));