单击按钮时更改 ImageView

Changing ImageView on button click(单击按钮时更改 ImageView)
本文介绍了单击按钮时更改 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

    }
}

推荐答案

声明你的a"全局来存储它的值.因为您在 onClick 期间将其初始化为 0.

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

}
}

这篇关于单击按钮时更改 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)