我正在创建一个 Android 程序,当单击按钮时,它会检查两个图像是否相同(ImageView
和 EmojiconTextView
)。 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/