gpt4 book ai didi

java - 按下按钮时无法加载随机图像

转载 作者:行者123 更新时间:2023-11-29 06:20:28 24 4
gpt4 key购买 nike

我想做的只是让按钮随机加载图像。但是,该按钮似乎不起作用。当我第一次在那里加载 Activity 时它工作正常并且有一个随机图像......但是当我按下按钮时它没有像我需要的那样加载另一个。知道我在这里有什么问题吗?我觉得不错:/

package com.my.package;


import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;

public class Randomimage extends Activity implements OnClickListener{


private Integer [] mImageIds = {
R.drawable.one,
R.drawable.two,
R.drawable.three,
};
private static final Random rgenerator = new Random();

private ImageView iv;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];

iv = (ImageView) findViewById(R.id.imageviewyeah);
iv.setImageResource(q);


View nextButton = findViewById(R.id.next_image_button);
nextButton.setOnClickListener(this);

}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next_image_button:
iv.setImageResource(rgenerator.nextInt(mImageIds.length));
break;
}

}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}

@Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.menu:
startActivity(new Intent(this, Main.class));
return true;
case R.id.startnhie:
startActivity(new Intent(this, startnhie.class));
return true;

}
return false;
}



}

最佳答案

在您的按钮按下处理代码中,更改

iv.setImageResource(rgenerator.nextInt(mImageIds.length));

iv.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);

关于java - 按下按钮时无法加载随机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3285698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com