简体   繁体   English

如何检查在ImageView Android中设置的图像

[英]How to check which image is set in imageview android

I have a star image in action bar. 我在动作栏中有一个星空图片。 When I click on it, then the image should change to ON star. 当我单击它时,图像应更改为ON星。 This is working fine. 一切正常。 But how to know whether image is at ON state or OFF state.I want something that, if the image is at OFF mode and user taps on ON star, then it should set to ON star image. 但是如何知道图像是处于ON状态还是OFF状态。我想要一些东西,如果图像处于OFF模式并且用户点击ON star,那么它应该设置为ON star image。 Initially, the image is set to OFF mode. 最初,图像设置为OFF模式。 So for this, I've wrote give line to turn on as user tap on it : 因此,为此,我编写了Give行以在用户点击时打开:

v.setBackgroundResource(android.R.drawable.star_big_on);

Guys pls suggest me for OFF mode if star image is already on. 伙计们建议我如果已经打开星图,请选择“关闭”模式。 I am not getting any idea. 我什么都不知道。

you can check it easily by setting tag of each image or by comparing there resource comparision of resource method is shown below: 您可以通过设置每个图像的标签或通过比较那里的资源比较来轻松检查它,资源比较方法如下所示:

if (regProfile.getDrawable().getConstantState() == 
    getResources().getDrawable(R.drawable.ivpic).getConstantState()){             
      Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
} else{
      Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
}

If my guess is correct you can achieve the functionality like this.change this code accordingly for image onclick 如果我的猜测是正确的,则可以实现这样的功能。针对图像onclick相应地更改此代码

 private Button button;
 public static boolean isclick=false;
 button.setOnClickListener(seathtlistner);
 private View.OnClickListener seathtlistner = new View.OnClickListener() {
 public void onClick(View v) {
// TODO Auto-generated method stub
  if(isclick){
  button.setBackgroundResource(R.drawable.onstarimage);
}else{
  button.setBackgroundResource(R.drawable.offstarimage);
}
isclick=!isclick;
 }

You can use Tag property. 您可以使用Tag属性。

img_view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Boolean tag_isOn = (Boolean) v.getTag();

                tag_isOn = !tag_isOn;
                v.setBackgroundResource(tag_isOn ?android.R.drawable.star_big_on:android.R.drawable.star_big_off);


                v.setTag(tag_isOn);

            }
        }); 
if (img_like.getTag() != null && img_like.getTag().toString().equals("red")) {
                        img_like.setImageResource(R.drawable.heart);
                        img_like.setTag("heart");
                    } else {
                        img_like.setImageResource(R.drawable.red);
                        img_like.setTag("red");
                    }`enter code here`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM