为什么如果语句不在Java/Android中工作?
我正在创建一个Android程序,当点击一个按钮时,它会检查这两个图像是否相同(ImageView
和EmojiconTextView
)。 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"/>
如果您需要了解这一问题的进一步帮助,请告诉我。
因为您在xml代码中使用了不同的标签。见下面
你在另一方面设置标签EmojiiconTextView作为
android:tag="myTag"
你作为
android:tag="myTag2"
上的ImageView设置标签和您比较使用相同方法的标签,
if (input.equals(input2)) {
checkingText.setText("Well Done!");
}
它们怎么可能是平等的? :)
希望是有道理的。
难道是new String("myTag").equals("myTag2")
给出错误吗? 尝试打印输入1和输入2以证实。
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!");
}
}
您正在比较标签而不是比较图像。
退房有关如何比较两个图像此链接: - compare two images in android
不幸的是,问题说一个摄像机图像,这些不是2个摄像机图像。 – MSP
像@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)
比较标签和你在xml中使用不同的标签 – DeKaNszn
那么标签必须相同吗? – MSP
我们明确地比较两个字符串,因此它们必须相同 –