点击imageview出现黄色环上的图片重新点亮消失?

时间:2016-10-17 10:22:53

标签: android button imageview

当我点击其中一个时,我有一个imageview当我选择另一个图像时,周围会出现黄色环视图2环到另一个环并从imageview 1中消失,带有imageview.setbackground(null)。 所以当我在带有黄色环的imageview2上回忆时,环就会像我选择另一个图像视图一样消失。

      public class MainActivity extends AppCompatActivity  implements View.OnClickListener {
ImageView imageView,imageView2,imageView3,imageView4,imageView5,
        imageView6,imageView7,imageView8,imageView9 ;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      imageView = (ImageView)findViewById(R.id.imageView);
    imageView2 = (ImageView)findViewById(R.id.imageView2);
    imageView3 = (ImageView)findViewById(R.id.imageView3);
    imageView4 = (ImageView)findViewById(R.id.imageView4);
    imageView5 = (ImageView)findViewById(R.id.imageView5);
    imageView6 = (ImageView)findViewById(R.id.imageView6);
    imageView7 = (ImageView)findViewById(R.id.imageView7);
    imageView8 = (ImageView)findViewById(R.id.imageView8);
    imageView9 = (ImageView)findViewById(R.id.imageView9);
    button =(Button)findViewById(R.id.button);
    button.setEnabled(false);


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isSelected(imageView)) {

                Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();

            } else if (isSelected(imageView2)) {
                Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();

            } else if (isSelected(imageView3)) {
                Toast.makeText(getApplicationContext(),"3",Toast.LENGTH_SHORT).show();

            }
            else if (isSelected(imageView4)) {
                Toast.makeText(getApplicationContext(),"4",Toast.LENGTH_SHORT).show();

            }
            else if (isSelected(imageView5)) {
                Toast.makeText(getApplicationContext(),"5",Toast.LENGTH_SHORT).show();

            }
            else if (isSelected(imageView6)) {
                Toast.makeText(getApplicationContext(),"6",Toast.LENGTH_SHORT).show();

            }
            else if (isSelected(imageView7)) {
                Toast.makeText(getApplicationContext(),"7",Toast.LENGTH_SHORT).show();

            }
            else if (isSelected(imageView8)) {
                Toast.makeText(getApplicationContext(),"8",Toast.LENGTH_SHORT).show();

            }
            else if (isSelected(imageView9)) {
                Toast.makeText(getApplicationContext(),"9",Toast.LENGTH_SHORT).show();

            }
        }
    });

}

private boolean isSelected(ImageView iv){
    Drawable background = iv.getBackground();
    if(background != null)

    return true;
    else 
        return false;

}


@Override
public void onClick(View v) {
    Drawable highlight = getResources().getDrawable(R.drawable.highlight);

    switch (v.getId()){
        case R.id.imageView :
            imageView.setBackground(highlight);
            imageView2.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            button.setEnabled(true);

            break;

        case R.id.imageView2 :
            imageView2.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);

            break;

        case R.id.imageView3 :
            imageView3.setBackground(highlight);
            imageView.setBackground(null);
            imageView2.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;


        case R.id.imageView4 :
            imageView4.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView2.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView5 :
            imageView5.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView2.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView6 :
            imageView6.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView2.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView7 :
            imageView7.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView2.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView8 :
            imageView8.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView2.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView9 :
            imageView9.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView2.setBackground(null);
            break;



    }
}

}

1 个答案:

答案 0 :(得分:0)

你几乎就在那里:

所有View都可以有背景,因此您可以安全地将方法isSelected()更改为接受View而不是ImageView s:

private boolean isSelected(View v){
    return ( v.getBackground() != null );
}

现在,首先检查onClick()View是否已被选中。在这种情况下,您只需将背景设置为null

@Override
public void onClick(View v) {

    if (isSelected(v)
    {
        v.setBackground(null);
        return;
    }

    Drawable highlight = getResources().getDrawable(R.drawable.highlight);

    switch (v.getId()){

         // this part remains unchanged
    }
}