安卓:检查是否值图片匹配 [英] Android: Checking if picture values match

查看:124
本文介绍了安卓:检查是否值图片匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图建立一个检查,看起来,看看是否当前设置的画面是x,如果它是X它的东西否则,如果其y它其他的东西。

Hi I'm trying to set up a check that looks to see if the currently set picture is x and if it is x it does stuff else if its y it does other stuff.

Button sharebtn = (Button)findViewById(R.id.Sharebtn);
    sharebtn.setOnClickListener(
       new View.OnClickListener()
       {
           public void onClick(View view)
           {
             ImageView picture = (ImageView) findViewById(R.id.BTN);
              if (picture.equals(R.drawable.x)){
                  //do really cool stuff
              }else if (picture.equals(R.drawable.y){
                   //do stuff
              }

           }
       });

本code按钮只是冻结,并没有做任何事情。它并不显示焦点或者仅。

with this code the button just freezes and doesn't do anything. It doesn't show focus either only.

编辑:更多信息
在code是用于收藏夹图标。因此,如果用户是标记为收藏我想要的图标是一个充满明星,否则空星在页面上。说我遇到的问题是我希望他们能够点击开始做code,要么添加页面,favs收藏或删除它。我认为这样做是通过检查,看看有什么图标目前其在OnCreate一套最简单的方法。图标正常工作,因此会显示充满​​如果一个最爱和viseversa然而code得到它删除或(从上面的代码段),工作不增加的收藏。我也试过:

more info The code is for a favorites icon. So if the user is on the page that is marked as a favorite I want the icon to be a filled star and otherwise an empty star. The problem that I'm having is i want them to be able to click the start to do the code that either adds the page to favs or removes it. I thought the easiest way to do that would be by checking to see what the icon currently is as its set in the oncreate. The icon works properly so it will show filled if a fav and viseversa however the code to get it delete or add to favs (the snippet from above) is not working. I've also tried:

    ImageView picture = (ImageView) findViewById(R.id.favsBTN);
            java.io.File file = new java.io.File(persistentCarsDir+"/"+vin);
            if (file.exists()) {
                removefavorite();
                picture.setImageResource(R.drawable.nostar);
            }else if(!file.exists()){
                favorite();
                picture.setImageResource(R.drawable.star);
            }

这code aboves作品^我只是文件路径错了:)

This code aboves works ^ i just had the file path wrong :)

推荐答案

在您的code您正在使用整数比较的ImageView的,这是行不通的。

In your code you are comparing an imageview with an integer, that won't work.

要得到你应该叫getDrawable一个的ImageView的绘制。并获得一个资源ID调用getResources实际的绘制()。getDrawable(R.drawable.x)...

To get the drawable of an ImageView you should call getDrawable. And to get the actual drawable for an resource id call getResources().getDrawable(R.drawable.x)...

不过,据我所知是没有办法比较可绘制...
最简单的方法很可能是使用setTag方法为ImageView的。所以每当你改变它绘制你可以设置它的标记X或Y...

But afaik there is no way to compare drawables... The easiest way would probably be to use the setTag method for the imageview. So you can set its tag to "X" or "Y" whenever you change its drawable...

然后检查标签,并决定该怎么做...

Then check the tag and decide what to do...

编辑:
您也可以使用复选框代替的ImageView的。你可以简单地检查器isChecked,以找出是否一直选中或未选中。
您可以更改复选框的图形与setButtonDrawable用你的明星。
如果你不想担任,而没有明星充满手动切换使用全状态的绘制,你可以有选中填充星和不是没有充满明星签...

Or you use a checkbox instead of the imageview. You can simply check "isChecked" to find out whether it's been checked or unchecked. You can change the graphic of the checkbox with "setButtonDrawable" to use your star. And if you don't want to manually switch between filled and not filled star use a statefull drawable where you can have the filled star for "checked" and the not filled star for not checked...

希望这有助于...

这篇关于安卓:检查是否值图片匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆