3

这是简短的版本。我有一个奖励活动,它有 25 个可点击的图像视图。我需要的是当单击一个 ImageView 时,所有其他的都需要设置为启用(假)。我想在 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);}
4

3 回答 3

1

初始化你ImageView ArrayonCreate(),然后只需使用下面的代码来禁用[启用 false] 每个 ImageView

for(int i = 0; i < imageview.length; i++)
{
    imageview[i].setEnabled(false);    
}
于 2012-06-20T01:05:01.657 回答
1
public void setEnabledFalse(int enableTrue){
for(int x = 0; x < imageview.length; x++){

   if(x != enabledTrue){
      imageview[i].setEnabled(false);
   }
}

}

创建一个类似上面的方法并为点击的 ImageView 传递一个参数。

于 2012-06-20T01:07:29.687 回答
1

这个怎么样,

  1. Activity 实现 View.OnClickListener
  2. 覆盖Activity中的onClick:
void onClick(View v) {
    for (int i = 0; i < imageview.length; i++) {
        if (v != imageview[i]) {
            imageview[i].setEnabled(false);
        }
    }
}

并且不要忘记在 Activity.onCreate 上设置 onclick 监听器

for (int i = 0; i < imageview.length; i++) {
   imageview[i].setOnClickListener(this);
}
于 2012-06-20T01:16:38.173 回答