我有两个按钮和一个 ImageView。当我单击下一步按钮时,它会从可绘制文件夹转到下一个图像。但是上一个按钮不起作用。我不知道为什么,我认为我的逻辑是正确的。这是我的代码:
public class Photo_gallery extends Activity implements OnClickListener{
Button next,previous;
ImageView slika;
int a = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
InicijalizujVarijable();
}
private void InicijalizujVarijable() {
next = (Button) findViewById(R.id.bNext);
previous = (Button) findViewById(R.id.bPrevious);
slika = (ImageView) findViewById(R.id.imageView1);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bNext:
if (a == 0)
{
slika.setImageResource(R.drawable.p2);
a = 1;
}
else if (a == 1)
{
slika.setImageResource(R.drawable.p3);
a = 2;
}
else if (a == 2)
{
slika.setImageResource(R.drawable.p4);
a = 3;
.
.
.
.
}
else if (a == 56)
{
startActivity(new Intent("com.example.apps.MENU"));
finish();
}
break;
case R.id.bPrevious:
a--;
next.performClick();
break;
}
}