2013-02-26 37 views
3

我的Android是新的,在网上找了一些很好的教程,所以我试图用一个if-else陈述一个简单的活动。我想“正确与错误”的提示/ Toast如果else语句中的Android

Button page1 = (Button) findViewById(R.id.button2); 
     page1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       final ImageView iv1 = (ImageView) findViewById(R.id.imageView1); 
       if (iv1.equals(R.drawable.airplane1)) { 
        Toast.makeText(getApplicationContext(), "Correct", 
          Toast.LENGTH_SHORT).show(); 
       } else if (iv1.equals(R.drawable.airplane2)) { 
        Toast.makeText(getApplicationContext(), "Please put an answer", 
          Toast.LENGTH_SHORT).show(); 
       } else if (iv1.equals(R.drawable.airplane3)){ 
        Toast.makeText(getApplicationContext(), "Wrong", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }); 

我不知道什么是错误的,我if-else声明,但从未提示都没有。我试图消除(iv1.equals(R.drawable.airplane3))(iv1.equals(R.drawable.airplane2))则只显示错误的吐司。我似乎无法做出正确的提示。

这里是我班的全码:

public class MainActivity extends Activity { 
private static final Random imagerandom = new Random(); 

private static final Integer[] Imagesnumber = 
    { R.drawable.airplane1, R.drawable.airplane2, R.drawable.airplane3, }; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Integer a = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)]; 
    final ImageView iv = (ImageView) findViewById(R.id.imageView1); 

    View nextButton = findViewById(R.id.button1); 
    nextButton.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View V) { 
       int resource = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)]; 
       iv.setImageResource(resource); 
     } 
    }); 
    Button page1 = (Button) findViewById(R.id.button2); 
     page1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       final ImageView iv1 = (ImageView) findViewById(R.id.imageView1); 
       if (iv1.equals(R.drawable.airplane1)) { 
        Toast.makeText(getApplicationContext(), "Correct", 
          Toast.LENGTH_SHORT).show(); 
       } else if (iv1.equals(R.drawable.airplane2)) { 
        Toast.makeText(getApplicationContext(), "Please put an answer", 
          Toast.LENGTH_SHORT).show(); 
       } else if (iv1.equals(R.drawable.airplane3)){ 
        Toast.makeText(getApplicationContext(), "Wrong", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

     }); 




} 

} 
+0

比较的int让我看看,如果我理解正确的话,你想看到的图像显示在YOUT' iv1'? – 2013-02-26 18:40:40

+0

是我真的想看看是否成像的使用,如果其他人我真的不太知道什么是错在IV1显示的是正确的还是错误的,如果我else语句 – NewbieontheSide 2013-02-26 18:42:21

+0

你缺少其他condition.i意味着其犯规的条件满足您指定 – 2013-02-26 18:44:10

回答

2
iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane1).getConstantState())                                                                                                            
+0

+1让我们知道....是否总是像这样工作?..我已经试过这个,并且正在工作 – Pragnani 2013-02-26 19:25:21

+0

在我的脑海里再次出现的一个问题是,这是否加载内存加载图像?因为我害怕我可能会在logcat中发生错误outofmemory错误 – NewbieontheSide 2013-02-26 19:32:02

2

R.drawable.airplane*是int,这意味着你用一个int,这是从来没有真实的比较对象的ImageView。使用iv.getDrawable().equals(getResources().getDrawable(R.drawable.airplane1));与Drawable对象进行比较。

这可能不是最好的选择性能明智的,你可能要跟踪所显示的图像作为一个类变量的指标,并以此为基础进行条件(或类似的规定)。

+0

你好invertigo i've试过你这个 提出什么“如果(iv1.getDrawable()== getResources()。getDrawable(R.drawable.airplane1)){ \t \t \t \t \t \t Toast.makeText( getApplicationContext(), “正确的”, \t \t \t \t \t \t \t \t Toast.LENGTH_SHORT)。显示(); \t \t \t \t \t \t \t \t \t \t \t} “ 我真的仍然得到了错误的吐司 – NewbieontheSide 2013-02-26 18:53:56

1

你不能用INT

iv1.equals(R.drawable.airplane1) 

类型比较ImageView的类型,这是错误的

试试这个

iv.getDrawable()==getResources().getDrawable(R.drawable.airplane1) 

代替

+0

你好Pragnani i've尝试过这种 ” 如果(iv.getDrawable()== getResources ().getDrawable(R.drawable.airplane1)){ \t \t \t \t \t \t Toast.makeText(getApplicationContext(), “正确的”, \t \t \t \t \t \t \t \t Toast.LENGTH_SHORT).show(); \t \t \t \t \t \t \t \t \t \t \t}否则如果(iv.getDrawable()== getResources()。getDrawable(R.drawable.airplane2)){ \t \t \t \t \t \t Toast.makeText(getApplicationContext (), “请把答案”, \t \t \t \t \t \t \t \t Toast.LENGTH_SHORT).show(); \t \t \t \t \t}否则如果(iv.getDrawable()== getResources()。getDrawable(R.drawable.airplane3)){ \t \t \t \t \t \t Toast.makeText(getApplicationContext(), “错误” 的, \t \t \t \t \t \t \t \t Toast.LENGTH_SHORT).show(); \t \t \t \t \t}” – NewbieontheSide 2013-02-26 18:59:49

+0

现在它不'吨提示任何事情,我想我在我的代码 – NewbieontheSide 2013-02-26 19:00:27

+0

@NewbieontheSide使用setTag和getTag对比,或setContentDescription和getContentDescription为imageviews .. – Pragnani 2013-02-26 19:07:53

0

您比较ImageView实例和int s,正确的方法来检查点击了哪个观点是:

if (iv1.getId() == R.id......) { ... 

这种方式,您与其他int