问题描述
想知道我是否以正确的方式进行.我的屏幕上有 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!