java - 检查ImageView是否等于输入点击
问题描述
我想创建一个 Android 应用程序(使用 Android Studio),用户将看到我在数组中生成的随机图像。然后,他们必须从与显示的随机图像相对应的水平滚动视图中选择正确的图像。但是,我似乎无法编写正确的代码来检查用户是否单击了正确的相应图像。我正在使用 if 语句。
这是我的 activity_main.xml:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="205dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="54dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/face_with_tears_of_joy"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedFaceWithTearsOfJoy"
android:clickable="true"
app:srcCompat="@drawable/face_with_tears_of_joy" />
<ImageView
android:id="@+id/grinning_face"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedGrinningFace"
android:clickable="true"
app:srcCompat="@drawable/grinning_face" />
<ImageView
android:id="@+id/grinning_face_with_smiling_eyes"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:clickable="true"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedGrinningFaceWithSmilingEyes"
app:srcCompat="@drawable/grinning_face_with_smiling_eyes" />
<ImageView
android:id="@+id/rolling_on_the_floor_laughing"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/rolling_on_the_floor_laughing" />
<ImageView
android:id="@+id/smiling_face_with_open_mouth"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/smiling_face_with_open_mouth" />
<ImageView
android:id="@+id/smiling_face_with_open_mouth_and_cold_sweat"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/smiling_face_with_open_mouth_and_cold_sweat" />
<ImageView
android:id="@+id/smiling_face_with_open_mouth_and_smiling_eyes"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
app:srcCompat="@drawable/smiling_face_with_open_mouth_and_smiling_eyes" />
</LinearLayout>
</HorizontalScrollView>
这是我的 MainActivity.java:
public class MainActivity extends AppCompatActivity {
public ImageView imageView;
public Random random;
public TextView textView;
int easy[] = {R.drawable.face_with_tears_of_joy, R.drawable.grinning_face, R.drawable.grinning_face_with_smiling_eyes};
int hard[] = {R.drawable.face_with_tears_of_joy, R.drawable.grinning_face, R.drawable.grinning_face_with_smiling_eyes,
R.drawable.rolling_on_the_floor_laughing, R.drawable.smiling_face_with_open_mouth, R.drawable.smiling_face_with_open_mouth_and_cold_sweat,
R.drawable.smiling_face_with_open_mouth_and_smiling_eyes};
public ImageView imageView1;
public ImageView imageView2;
public ImageView imageView3;
public ImageView imageView4;
public ImageView imageView5;
public ImageView imageView6;
public ImageView imageView7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.face_with_tears_of_joy);
imageView2 = (ImageView) findViewById(R.id.grinning_face);
imageView3 = (ImageView) findViewById(R.id.grinning_face_with_smiling_eyes);
imageView4 = (ImageView) findViewById(R.id.rolling_on_the_floor_laughing);
imageView5 = (ImageView) findViewById(R.id.smiling_face_with_open_mouth);
imageView6 = (ImageView) findViewById(R.id.smiling_face_with_open_mouth_and_cold_sweat);
imageView7 = (ImageView) findViewById(R.id.smiling_face_with_open_mouth_and_smiling_eyes);
imageView = (ImageView) findViewById(R.id.imageView);
random = new Random();
textView = (TextView) findViewById(R.id.textView);
int randomInt = random.nextInt(hard.length);
imageView.setBackgroundResource(hard[randomInt]);
}
public void clickedEmoji(View view) {
ImageView image = (ImageView) view;
Bitmap clicked = ((BitmapDrawable) image.getDrawable()).getBitmap();
Bitmap displayed = ((BitmapDrawable) image.getDrawable()).getBitmap();
if (clicked == displayed) {
textView.setText("Well Done!");
} else {
textView.setText("Unlucky!");
}
}
解决方案
您必须实现 OnClickListener 并将 OnClickListener 设置为要检查的已单击的每个图像。检查此链接可能会有所帮助.. Android Studio 如何检查单击了哪个 Imageview 并为其分配 Int 值?
推荐阅读
- python - 在循环中读取 csv 在不匹配的行处停止
- c - 为什么不能使用 malloc 调整 C 中的数组大小?
- c# - signin-oidc 重定向不起作用 OpenId Connect
- c# - 使用 ListView 时,如何在 Xamarin Forms 中设置自定义控件绑定到父视图模型属性?
- c++ - FREAK 不是 cv 的成员?
- angular - Angular 2+ HttpErrorResponse 超时未返回正确状态
- java - Spring-boot & Java FindById 返回 null 而不是数字
- html - CSS 特定问题
- java - MicroMeter:删除/禁用某个标签
- swift - iOS12:transitionController 已弃用