2013-02-26 199 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