首页 > 解决方案 > 如何获取 ImageView 值

问题描述

我正在尝试制作一个随机图像问答游戏,其中 100 个随机汽车图像将显示在 imageView 中,用户将从微调器中选择,例如“Audi”、“BMW”等,具体取决于汽车的品牌正在显示。

我已经创建了随机图像和微调器部分。我的问题是如何获取 imageView ID,以便我可以使用 if 语句来查看现在显示的图像是否是用户从微调器中选择的图像

public class identifyTheBrand extends AppCompatActivity {
    ImageView imageView;
    Button btn;
    HashMap<String[], Integer> map = new HashMap<String[], Integer>();

    images img1 = new images(R.drawable.car1);
    images img2 = new images(R.drawable.car2);
    images img3 = new images(R.drawable.car3);
    images img4 = new images(R.drawable.car4);
    images img5 = new images(R.drawable.car5);
    images img6 = new images(R.drawable.car6);



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identify_the_brand);

        imageView = (ImageView) findViewById(R.id.imageViewCars);
        btn = (Button) findViewById(R.id.next);
        Spinner dropDown = (Spinner) findViewById(R.id.dropDown);

        //Make array adapter - this is a container that will hold values and integrate them within the spinner/dropdown
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(identifyTheBrand.this,
                android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.brand));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //Specify that this adapter will have a drop down list
        dropDown.setAdapter(myAdapter); //Shows the data from the array list into the drop down list/spinner

        showRandomImage();

        //Button which shows the next random image
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             showRandomImage();
           }
        });

    }

    public void showRandomImage(){
        shuffleImage();
        imageView.setImageResource(allImages[0].getCarImage());
    }

    images [] allImages = new images[] {
            img1,img2,img3,img4,img5,img6
    };

    images [] audi = new images[] {
            img1,img2
    };

    public void shuffleImage(){
        Collections.shuffle(Arrays.asList(allImages));
    }

设置者和获取者:

public class images {

    private int carImage;

    public images(int carImage) {
        this.carImage = carImage;
    }

    public int getCarImage() {
        return carImage;
    }

    public void setCarImage(int carImage) {
        this.carImage = carImage;
    }
}

字符串.xml

<string-array name="brand">
        <item>Audi</item>
        <item>Bently</item>
        <item>BMW</item>
        <item>Fiat</item>
        <item>Ford</item>
        <item>Honda</item>
        <item>Hyundai</item>
        <item>Jaguar</item>
        <item>Mercedes Benz</item>
        <item>Toyota</item>
    </string-array>

我还没有完成我的代码,但我主要专注于如何解决我的问题,到目前为止我还没有任何进展。

标签: javaandroid-studio

解决方案


推荐阅读