2017-07-13 19 views
0

我正在創建一個Android程序,當點擊一個按鈕時,它會檢查這兩個圖像是否相同(ImageViewEmojiconTextView)。 EmojiconTextView來自我在我的應用程序中使用的庫。爲什麼如果語句不在Java/Android中工作?

public void clickedCheck(View view) { 

    String input = emojiconTextView.getTag().toString(); 
    String input2 = myRandomImage.getTag().toString(); 

    if (input.equals(input2)) { 
     checkingText.setText("Well Done!"); 
    } else { 
      checkingText.setText("Unlucky!"); 
    } 
} 

但是,當我點擊按鈕時,即使圖像相同,程序也顯示「不幸」。所以它完全無視我的'如果陳述'。

這是我ImageView屬性:

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/myRandomImage" 
     android:tag="myTag2" 
     android:layout_above="@+id/emojicon_text_view" 
     android:layout_centerHorizontal="true"/> 

而這是從我EmojiconTextView

<hani.momanii.supernova_emoji_library.Helper.EmojiconTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/emojicon_text_view" 
     android:textColor="@android:color/black" 
      android:textAppearance="@style/TextAppearance.AppCompat.Large" 
     android:text="Emojicon Text View" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:tag="myTag" 
     android:layout_marginTop="26dp"/> 

如果您需要了解這一問題的進一步幫助,請告訴我。

+1

比較標籤和你在xml中使用不同的標籤 – DeKaNszn

+0

那麼標籤必須相同嗎? – MSP

+1

我們明確地比較兩個字符串,因此它們必須相同 –

回答

3

因爲您在xml代碼中使用了不同的標籤。見下面

你在另一方面設置標籤EmojiiconTextView作爲

android:tag="myTag" 

你作爲

android:tag="myTag2" 

上的ImageView設置標籤和您比較使用相同方法的標籤,

if (input.equals(input2)) { 
    checkingText.setText("Well Done!"); 
} 

它們怎麼可能是平等的? :)

希望是有道理的。

+0

那麼你是說我的標籤必須是相同的嗎? – MSP

+0

我不是說他們必須是平等的。我指着你正在做的錯事。您的測試條件聲明表明它們必須相同才能通過相同的測試。 –

+0

哦,我明白了。那麼是否有正確的方法來做到這一點,並讓他們進行比較? :) – MSP

0

難道是new String("myTag").equals("myTag2")給出錯誤嗎? 嘗試打印輸入1和輸入2以證實。

0
public void clickedCheck(View view) { 

    int input = Integer.parseInt(emojiconTextView.getTag()); 
    int input2 = Integer.parseInt(myRandomImage.getTag()); 

    if (input == input2) { 
     checkingText.setText("Well Done!"); 
    } else { 
     checkingText.setText("Unlucky!"); 
    } 
} 
+0

它說「不兼容的類型」 – MSP

+0

我編輯了我的答案,試試它可能對你有所幫助。 –

+0

我試過你編輯的答案,它在整數中的'parseInt(java.lang.String)不能應用於(java.lang.Object) – MSP

0

您正在比較標籤而不是比較圖像。

退房有關如何比較兩個圖像此鏈接: - compare two images in android

+0

不幸的是,問題說一個攝像機圖像,這些不是2個攝像機圖像。 – MSP

1

像@AbulWaheed說,因爲你設置你的XML資源文件裏的標籤,每次你做

if (input.equals(input2)) { 
    checkingText.setText("Well Done!"); 
} else { 
     checkingText.setText("Unlucky!"); 
} 

你會總是比較你的XML文件中設置的標籤。爲了解決這個問題,你需要設置在Java代碼中顯示ImageView的drawable嗎?

如果你是,你也可以設置標記爲imageview.setTag("tagName")因爲我假設emojiView和imageView。

這樣,你可以有某種地圖HashMap<Drawable, String> map,當你選擇你要在你的圖像視圖中使用的Drawable時,你自動設置標籤。

String tag = map.get(Drawable) 
imageview.setTag(tag) 
相關問題