反正比较INT要绘制的?ANDROID [英] Anyway to compare int to drawable?ANDROID

查看:107
本文介绍了反正比较INT要绘制的?ANDROID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正是有比较s到一个?在这种code我的int作为答案,如果绘制==小号然后我要显示一个正确!敬酒消息。任何帮助将是AP preciated。

  btn1.setOnClickListener(新OnClickListener()
{
    @覆盖
    公共无效的onClick(视图v)
    {
          int类型= R.drawable.exitbtn;
          资源RES = getResources();
          可绘制绘制= res.getDrawable(R.drawable.exitbtn);
          对象A =绘制;
          若(a == S)
          {
                ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView1);
                Toast.makeText(getApplicationContext(),正确!,Toast.LENGTH_SHORT).show();
                imageview.setImageDrawable(绘制);
                btn1.setText(1);
                btn2.setText(2);
                btn3.setText(3);
                btn4.setText(4);
         }
    }
}
 

解决方案

绘制对象实例相比于 INT 没有任何意义。什么是你想怎么办?是否要检查方法 res.getDrawable(R.drawable.exitbtn); 返回正确的对象

我认为这是多余的获取从R-文件标识符:

  int类型= R.drawable.exitbtn;
 

然后使用同样的标识获取该对象:

 可绘制绘制= res.getDrawable(R.drawable.exitbtn);
 

,然后尝试检查它是否是真正合适的对象通过比较IDS。

我认为有对错误没有地方(特别是,一个int值的混乱),当它涉及到由ID获取一个绘制对象。如果你想确保对象被创建,检查对空。

 可绘制绘制= res.getDrawable(R.drawable.exitbtn);
如果(绘制!= NULL){
   //做的东西
} 其他 {
   //处理情况没有绘制
}
 

另外,我觉得没有办法从可绘制对象获取的ID。通过 getDrawable 返回的实例并不包含这些信息。

更重要的是,你可以使用一个事实,即 getDrawable 可以抛出一个 NotFoundException 如果ID是有点不对。检查文档了解详细信息。

Is there anyway to compare s to a? In this code I have int s as the answer and if the drawable == s then I want to display a "Correct!" toast message. Any help would be appreciated.

btn1.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
          int s = R.drawable.exitbtn;
          Resources res = getResources();
          Drawable drawable = res.getDrawable(R.drawable.exitbtn);
          Object a=drawable;
          if(a==s)
          {
                ImageView imageview =(ImageView)findViewById(R.id.imageView1);
                Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
                imageview.setImageDrawable(drawable);
                btn1.setText("1");
                btn2.setText("2");
                btn3.setText("3");
                btn4.setText("4");
         }
    }
}

解决方案

Comparing a Drawable instance to an int doesn't make sense. What are you trying to do? Do you want to check if the method res.getDrawable(R.drawable.exitbtn); returns the right object?

I think it's redundant to fetch an identifier from R-file:

int s = R.drawable.exitbtn;

Then fetch the object using the very same identifier:

Drawable drawable = res.getDrawable(R.drawable.exitbtn);

And then try to check whether it's really the right object by comparing ids.

I think that there's no place for mistake (specifically, confusion of an int value) when it comes to getting a Drawable by id. If you want to be sure the object has been created, check it against a null.

Drawable drawable = res.getDrawable(R.drawable.exitbtn);
if(drawable != null){
   //do stuff
} else {
   //handle the situation where there's no drawable
}

Also, I think there's no way to fetch an id from a Drawable object. The instance returned by getDrawable does not contain such information.

Better yet, you can use the fact that getDrawable can throw a NotFoundException if the id is somehow wrong. Check the docs for details.

这篇关于反正比较INT要绘制的?ANDROID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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