Changing ImageView on button click

丶灬走出姿态 提交于 2019-11-28 10:36:51

问题


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;  
        }

    }
}

回答1:


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;
}

}
}


来源:https://stackoverflow.com/questions/3490953/changing-imageview-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!