我想创建一个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!");
}
}
答案 0 :(得分:0)
您必须实现OnClickListener并仅将OnClickListener设置为要检查的每个已单击图像。 检查此链接可能会有所帮助。 Android Studio How to check which Imageview has clicked and assign an Int value to it?
答案 1 :(得分:0)
我不会做多个功能,而是将它们全部组合为一个。另外,您创建的代码会将ImageView与Drawable进行比较,这是无效的比较。相反,您可以执行以下操作。
public void clickedImage(View view) {
ImageView image = (ImageView) view;
Bitmap clicked = drawableToBitmap(image.getDrawable());
Bitmap displayed = drawableToBitmap(imageView.getBackground());
if (clicked == displayed) {
textView.setText("Well Done!");
} else {
textView.setText("Unlucky!");
}
}
public Bitmap drawableToBitmap(Drawable d) {
Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
d.setBounds(0, 0, c.getWidth(), c.getHeight());
d.draw(c);
return b;
}
然后,对于XML文件中的每个ImageView,只需添加android:onClick="clickedImage"
。希望这会有所帮助!