gpt4 book ai didi

java - 如何制作 ImageView 数组并在 for 循环中将它们设置为 enabled(false)

转载 作者:搜寻专家 更新时间:2023-11-01 09:07:01 24 4
gpt4 key购买 nike

这是简短的版本。我有一个 Bonus Activity,它有 25 个可点击的 ImageView 。我需要的是单击一个 ImageView 时,所有其他 ImageView 都需要设置为启用(false)。我想在 for 循环中执行此操作。下面是我所拥有的示例。任何建议

    private ImageView img1;
private ImageView img2;
private ImageView img3;
private ImageView img4;
private ImageView img5;
private ImageView img6;
private ImageView img7;
private ImageView img8;
private ImageView img9;
private ImageView img10;
private ImageView[] imageview = {img1,img2,img3,img4,img5,img6,img7,img8,img9,img10};
int i = 0;
int imgv =0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.bonus_layout);


img1 = (ImageView) findViewById(R.id.imageView1);
img2 = (ImageView) findViewById(R.id.imageView2);
img3 = (ImageView) findViewById(R.id.imageView3);
img4 = (ImageView) findViewById(R.id.imageView4);
img5 = (ImageView) findViewById(R.id.imageView5);
img6 = (ImageView) findViewById(R.id.imageView6);
img7 = (ImageView) findViewById(R.id.imageView7);
img8 = (ImageView) findViewById(R.id.imageView8);
img9 = (ImageView) findViewById(R.id.imageView9);
img10 = (ImageView) findViewById(R.id.imageView10);


img1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(rank[b]==0){

for(i<25) imageview.get(i) setEnabled(false);
(i++);



//This example offered get a force close for(ImageView iv:imageview)iv.setEnabled(false);
}
}} });

这是你们帮我改正的代码

public class BonusActivity extends Activity implements View.OnClickListener   
{
Integer rank[]= {0,100,200,300,0,400,500,0};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.bonus_layout);

final ImageView img1 = (ImageView) findViewById(R.id.imageView1);
final ImageView img2 = (ImageView) findViewById(R.id.imageView2);
final ImageView img3 = (ImageView) findViewById(R.id.imageView3);
final ImageView img4 = (ImageView) findViewById(R.id.imageView4);
final ImageView img5 = (ImageView) findViewById(R.id.imageView5);
final ImageView img6 = (ImageView) findViewById(R.id.imageView6);
final ImageView img7 = (ImageView) findViewById(R.id.imageView7);
final ImageView img8 = (ImageView) findViewById(R.id.imageView8);
final ImageView img9 = (ImageView) findViewById(R.id.imageView9);
final ImageView img10 = (ImageView) findViewById(R.id.imageView10);
final ImageView[] imageview ={img1,img2,img3,img4,img5,img6,img7,img8,img9,img10};

img1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

if(rank[b]==0){
img1.setImageResource(R.drawable.empty);

for(int i = 0; i<imageview.length;i++)
{imageview[i].setClickable(false);}

最佳答案

onCreate() 中初始化您的 ImageView Array,然后只需使用下面的代码来禁用 [enable false] 每个 ImageView

for(int i = 0; i < imageview.length; i++)
{
imageview[i].setEnabled(false);
}

关于java - 如何制作 ImageView 数组并在 for 循环中将它们设置为 enabled(false),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11111496/

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