动态更改按钮上的Imageview [英] Changing Imageview on button dynamically

查看:56
本文介绍了动态更改按钮上的Imageview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有按钮和ImageView.

In my application, I have button and ImageView.

在这里,当我按下按钮时,我想更改ImageView.我的可绘制文件夹中有5张图像.按下按钮时,ImageView会基于按钮的单击来一张一张地更改图像.我想要解决方案.

Here when i press button i want to change ImageView. I have 5 images in my drawable folder. On press button ImageView changes images one by one based on button click. I want it's solution.

感谢任何可以提供帮助的人.

Grateful to anyone that can help.

推荐答案

维护图像ID数组,并在onClick中,使用该数组中的ID设置图像,然后递增索引.

Maintain an array of image ids and inside onClick, set images using id from the array, then increment index.

例如:-

ArrayList<Integer> ids=new ArrayList<Integer>();
        ids.add(R.drawable.image1);
        ids.add(R.drawable.image2);
        ids.add(R.drawable.image3);
        ids.add(R.drawable.image4);
        ids.add(R.drawable.image5);
Int index=0
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
if(index<ids.size()){
      imageview.setImageResource(ids.get(index));
       index++;
   }
 else index=0;
 }
 });

这篇关于动态更改按钮上的Imageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆