Код: Выделить всё
public class MainActivity extends AppCompatActivity {
private static ImageView andro;
private static Button buttonswitch;
int current_image_index = 0;
int[] images = {
R.mipmap.andro_img,
R.mipmap.apple_image,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher_round
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
public void buttonClick() {
andro = (ImageView) findViewById(R.id.imageView);
buttonswitch = (Button) findViewById(R.id.button);
buttonswitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
current_image_index++;
current_image_index = current_image_index % images.length;
andro.setImageResource(images[current_image_index]);
}
});
}
}
Код: Выделить всё
@Override
public void onClick(View view) {
current_image_index++;
current_image_index = current_image_index % images.length;
andro.setImageResource(images[current_image_index]);
}
это будет повторяться снова и снова, поскольку current_image_index остается равным 0. Тогда как изображение может постоянно меняться после щелчка по нему, поскольку current_image_index % image.length всегда будет давать результат 0.
Подробнее здесь: https://stackoverflow.com/questions/447 ... rray-index
Мобильная версия