java - 为什么 if 语句在 Java/Android 中不起作用?

标签 java android if-statement tags android-imageview

我正在创建一个 Android 程序,当单击按钮时,它会检查两个图像是否相同(ImageViewEmojiconTextView)。 EmojiconTextView 来 self 在应用程序中使用的库。

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!");
    }
}

但是,当我单击该按钮时,即使图像相同,程序也会显示“Unlucky”。所以它完全忽略了我的“if 语句”。

这是我的 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"/>

这是来 self 的 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"/>

如果您需要进一步帮助来理解该问题,请告诉我。

最佳答案

因为您在 xml 代码中使用了不同的标签。见下文

您正在将标签 EmojiiconTextView 设置为

android:tag="myTag"

另一方面,您将 ImageView 上的标记设置为

android:tag="myTag2"

并且您正在使用相同的方法来比较标签

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

它们怎么可能相等呢? :)

希望这是有道理的。

关于java - 为什么 if 语句在 Java/Android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45081197/

相关文章:

java - 当我注销 Facebook Android Studio 时,我需要一个按钮消失

Android 应用程序因 BaseGameActivity 崩溃

android - 我想删除数据,直到数据库达到特定大小

c - C 中的 while 循环中的 if

ios - Playground 执行失败 : <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType'

从 HTTPS 服务器运行的 Java Web Start 应用程序被 Java 安全性阻止

java - Intellij Idea Plugin获取虚拟文件的类文件

Java无法解密PHP中加密的字符串

java - 在 Hibernate 4.2.21 中删除表的所有行

ios - 编程 IBAction 按钮打开/关闭 map 层