Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

android - Changing ImageView on button click

Wondering if I'm going about this the right way or not. I have 3 buttons on my screen (Restart, Previous, Next). When the view loads it shows the first image which is fine. When I click the "Next" button I want it to load a second image and so on for up to 9 images. If I click the "Previous" button it should go back one image. Clicking "Restart" should go to the first image. I have the Restart one working. I'm having trouble with the Next button because it only shows the second image (I think because my "a" variable is initialized at 0). Here's my code. Grateful to anyone that can help.

public class Story1 extends Activity implements View.OnClickListener {

    ImageView image = (ImageView) findViewById(R.id.story1_1);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.story1);

        Button restart = (Button) findViewById(R.id.restart);
        restart.setOnClickListener(this);

        Button previous = (Button) findViewById(R.id.previous);
        previous.setOnClickListener(this);

        Button next = (Button) findViewById(R.id.next);
        next.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) 
    {
        int a = 0;

        switch (view.getId())
        {
            case R.id.restart:
                image.setImageResource(R.drawable.story1_1);
                break;

            case R.id.next:
                if (a == 0)
                {
                    image.setImageResource(R.drawable.story1_2);
                    a = 1;
                }
                else if (a == 1)
                {
                    image.setImageResource(R.drawable.story1_3);
                    a = 2;
                }
                else if (a == 2)
                {
                    image.setImageResource(R.drawable.story1_4);
                    a = 3;
                }
                else if (a == 3)
                {
                    image.setImageResource(R.drawable.story1_5);
                    a = 4;
                }
                else if (a == 4)
                {
                    image.setImageResource(R.drawable.story1_6);
                    a = 5;
                }
                else if (a == 5)
                {
                    image.setImageResource(R.drawable.story1_7);
                    a = 6;
                }
                else if (a == 6)
                {
                    image.setImageResource(R.drawable.story1_8);
                    a = 7;
                }
                else if (a == 7)
                {
                    image.setImageResource(R.drawable.story1_9);
                    image.setClickable(false);
                }
                break;  
        }

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

declare your "a" global to store its value. Because your initializing it to 0 during onClick.

public class Story1 extends Activity implements View.OnClickListener 
{

ImageView image = (ImageView) findViewById(R.id.story1_1);
Button next;
int a = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.story1);

Button restart = (Button) findViewById(R.id.restart);
restart.setOnClickListener(this);

Button previous = (Button) findViewById(R.id.previous);
previous.setOnClickListener(this);

next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);

}


@Override
public void onClick(View view) 
{

switch (view.getId())
{
    case R.id.restart:
        image.setImageResource(R.drawable.story1_1);
        a = 0;
        break;

    case R.id.next:
        if (a == 0)
        {
            image.setImageResource(R.drawable.story1_2);
            a = 1;
        }
        else if (a == 1)
        {
            image.setImageResource(R.drawable.story1_3);
            a = 2;
        }
        else if (a == 2)
        {
            image.setImageResource(R.drawable.story1_4);
            a = 3;
        }
        else if (a == 3)
        {
            image.setImageResource(R.drawable.story1_5);
            a = 4;
        }
        else if (a == 4)
        {
            image.setImageResource(R.drawable.story1_6);
            a = 5;
        }
        else if (a == 5)
        {
            image.setImageResource(R.drawable.story1_7);
            a = 6;
        }
        else if (a == 6)
        {
            image.setImageResource(R.drawable.story1_8);
            a = 7;
        }
        else if (a == 7)
        {
            image.setImageResource(R.drawable.story1_9);
            image.setClickable(false);
        }
        break;  
    case R.id.previous:
            a--;
            next.performClick();
        break;
}

}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...