java - 按下android中的图像按钮并按顺序获取数组中的随机图像

标签 java android imagebutton android-imagebutton

public abstract class MainActivity extends Activity  implements View.OnClickListener{

    private ImageButton bt1;
    public int index =0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Drawable[][] images = new Drawable[4][16];
        bt1 = (ImageButton) findViewById(R.id.im_bt1);
        bt1.setImageDrawable(images[index][(int)(Math.random()*16)]);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                ((ImageButton)v).setImageDrawable(images[index][(int)(Math.random()*16)]);

            }
        });

    }


    Red RR1 = new Red(R.drawable.rr1);
    Red RY1 = new Red(R.drawable.ry1);
    Red RG1 = new Red(R.drawable.rg1);
    Red RB1 = new Red(R.drawable.rb1);
    Green GB1 = new Green(R.drawable.greenblue);
    Green GG1 = new Green(R.drawable.greengreen);
    Green GR1 = new Green(R.drawable.greenred);
    Green GY1 = new Green(R.drawable.greenyellow);
    Yellow YY1 = new Yellow(R.drawable.yellowyellow);
    Yellow YB1 = new Yellow(R.drawable.yellowblue);
    Yellow YG1 = new Yellow(R.drawable.yellowgreen);
    Yellow YR1 = new Yellow(R.drawable.yellowred);
    Blue BR1= new Blue(R.drawable.br1);
    Blue BB1=new Blue(R.drawable.bb1);
    Blue BY1=new Blue(R.drawable.by1);
    Blue BG1= new Blue(R.drawable.bg1);

    Red[] Redarray = new Red[]{
            RR1,RY1,RG1,RB1
    };
    Green[] Greenarray = new  Green[]{
            GR1,GB1,GY1,GG1
    };

    Blue[] Bluearray = new  Blue[]{
      BB1,BY1,BG1,BR1
    };

    Yellow[] Yellowarray = new Yellow[]{
            YB1,YG1,YR1,YY1
    };


}

我的布局中有一个 ImageButton ,我想按下该按钮并从数组中获取另一个随机 ImageButton ,然后在按下该按钮时获取另一个,但是我希望它在我制作的数组上按顺序显示。

图像数组 1 图像数组 2 图像数组 3 图像数组 4

图像按钮从array1到另一个随机图像按钮从array2到另一个图像按钮从array 3...

编辑:所以我创建了一个所有数组的类,但它不起作用..我正在努力创建一个数组数组..你们可以检查我的代码吗?谢谢..

最佳答案

首先:我不会每次都更改为另一个 ImageButton,而只是更改显示的图像。

第二:您需要一个 ImageButtons(或可绘制对象)数组的数组。 然后,您需要外部数组的索引,并需要内部数组的随机值。

Drawable[][] images = new Drawable[4][10]; //your 4 outer arrays with 10 drawables to choose randomly from // needs to be a member variable
fillArray();//add your drawables to the arrays here
int index = 0;//needs to be a member variable too
ImageButton button = new ImageButton(this);
button.setImageDrawable(images[index][(int)(Math.random()*10)]);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    index++;
        ((ImageButton)v).setImageDrawable(images[index][(int)(Math.random()*10)]);
    }
});

关于java - 按下android中的图像按钮并按顺序获取数组中的随机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48222782/

相关文章:

java - 通过列表 Activity 启动 Activity

java - 单击按钮事件时如何在对话框中设置图标(图像)

Android 使用带有 onContentChanged() 的自定义 CursorAdapter

xaml - UWP xaml : How to display a button with icon and text in it?

android - 移除ImageButton的图片资源

java - PropertyPlaceholderConfigurer 位置内的 Spring 占位符

android - 评分栏像代号一的android

android - 管理堆大小

android - 添加具有透明背景的图像按钮

java - Json 和抽象类 'Can not construct instance'