Changing ImageView on button click

十年热恋 提交于 2019-11-29 16:47:01

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

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