早上好,我无法比较我在Android Studio中制作的Android应用程序中的多个Drawable。我已经在Internet上进行了搜索,但似乎找不到正确的分辨率。
用户单击图像时,必须检查以查看当前是否正在显示同一图像(R.drawable),该图像应该随机显示在imageView
中。
加载Activity
时,它将显示imageView
内部数组中的随机图像。但是,当用户单击正确的对应图像时,它将不会比较两个Drawable。
用户单击imageView
中的horizontal scroll layout
时,必须检查以查看他们单击的imageView
是否与所显示的相同。
< br />
我也知道我正在比较imageView
和Int
我已经考虑过只创建1个onClick
方法来比较Drawable,但是不确定如何做到这一点。请任何人帮助,将不胜感激!
我的MainActivity:
public class MainActivity extends AppCompatActivity {
public ImageView imageView;
public Random random;
public TextView textView;
int easy[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int hard[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3,
R.drawable.img4, R.drawable.img5, R.drawable.img6,
R.drawable.img7};
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);
random = new Random();
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.imageView);
imageView1 = (ImageView) findViewById(R.id.IMG1);
imageView2 = (ImageView) findViewById(R.id.IMG2);
imageView3 = (ImageView) findViewById(R.id.IMG3);
imageView4 = (ImageView) findViewById(R.id.IMG4);
imageView5 = (ImageView) findViewById(R.id.IMG5);
imageView6 = (ImageView) findViewById(R.id.IMG6);
imageView7 = (ImageView) findViewById(R.id.IMG7);
int randomInt = random.nextInt(hard.length);
imageView.setBackgroundResource(hard[randomInt]);
}
public void clickedIMG1(View view) {
if (imageView.equals(R.drawable.img1)) {
textView.setText("Image 1 is displayed");
}
}
public void clickedIMG2(View view) {
if (imageView.equals(R.drawable.img2)) {
textView.setText("Image 2 is displayed");
}
}
public void clickedIMG3(View view) {
if (imageView.equals(R.drawable.img3)) {
textView.setText("Image 3 is displayed");
}
}
public void clickedIMG4(View view) {
if (imageView.equals(R.drawable.img4)) {
textView.setText("Image 4 is displayed");
}
}
public void clickedIMG5(View view) {
if (imageView.equals(R.drawable.img5)) {
textView.setText("Image 5 is displayed");
}
}
public void clickedIMG6(View view) {
if (imageView.equals(R.drawable.img6)) {
textView.setText("Image 6 is displayed");
}
}
public void clickedIMG7(View view) {
if (imageView.equals(R.drawable.img7)) {
textView.setText("Image 7 is displayed");
}
}
}
我的activity_main:
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="210dp"
android:layout_height="177dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="89dp"
android:layout_marginTop="111dp"
android:contentDescription="@string/funny_emoji"
tools:layout_editor_absoluteX="120dp"
tools:layout_editor_absoluteY="232dp" />
<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/IMG1"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:clickable="true"
android:onClick="clickedIMG1"
android:src="@drawable/img1" />
<ImageView
android:id="@+id/IMG2"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:clickable="true"
android:onClick="clickedIMG2"
android:src="@drawable/img2" />
<ImageView
android:id="@+id/IMG3"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:clickable="true"
android:onClick="clickedIMG3"
android:contentDescription="@string/funny_emoji"
android:src="@drawable/img3" />
<ImageView
android:id="@+id/IMG4"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/funny_emoji"
android:onClick="clickedIMG4"
android:src="@drawable/img4" />
<ImageView
android:id="@+id/IMG5"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:onClick="clickedIMG5"
android:contentDescription="@string/funny_emoji"
android:src="@drawable/img5" />
<ImageView
android:id="@+id/IMG6"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:onClick="clickedIMG6"
android:contentDescription="@string/funny_emoji"
android:src="@drawable/img6" />
<ImageView
android:id="@+id/IMG7"
android:layout_width="50dp"
android:layout_height="55dp"
android:layout_marginLeft="5dp"
android:onClick="clickedIMG7"
android:contentDescription="@string/funny_emoji"
android:src="@drawable/img7" />
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="154dp"
android:layout_marginTop="21dp"
android:text="TextView" />
</android.widget.RelativeLayout>
答案 0 :(得分:0)
您可以使用可绘制资源ID设置imageview标记,例如
imageView.setTag(hard[randomInt]);
设置背景资源后。
然后在比较时就可以做到
if (Integer.valueOf(imageView.getTag().toString) == R.drawable.img1){
}
//same way compare with other images R.drawable.img2, R.drawable.img3
答案 1 :(得分:0)
您可以从ImageView获得Drawable
赞
Drawable myDrawable = iv.getDrawable();
OR
您可以比较ImageView中的Drawable
赞
if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
//do work here
}
您的情况
您可以通过
轻松进行比较Drawable myDrawable = getResources().getDrawable(hard[randomInt]);
然后从drawable而不是ImageView进行比较
if(myDrawable==getResources().getDrawable(R.drawable.image1)){
//do work here
}