单击按钮更改ImageView

时间:2010-08-16 06:08:03

标签: android imageview

想知道我是否以正确的方式解决这个问题。我的屏幕上有3个按钮(重启,上一个,下一个)。当视图加载时,它显示第一个图像很好。当我单击“下一步”按钮时,我希望它加载第二个图像,依此类推最多9个图像。如果我单击“上一步”按钮,它应该返回一个图像。单击“重新启动”应转到第一张图像。我有一个重启工作。我在使用Next按钮时遇到问题,因为它只显示第二个图像(我想因为我的“a”变量初始化为0)。这是我的代码。感谢任何有帮助的人。

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 个答案:

答案 0 :(得分:2)

声明你的“a”全局来存储它的值。因为你在onClick期间将它初始化为0。

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

}
}