慕碼人8056858
2023-05-17 14:33:10
我正在測(cè)試這段生成隨機(jī)圖像的代碼,我試圖讓每個(gè)生成的圖像都有一個(gè)描述Button buttong;Random r;Integer[] images = { R.drawable.img250, R.drawable.img484, R.drawable.img485,}; imageView = (ImageView) findViewById(R.id.imageView); message = (TextView) findViewById(R.id.text_view); buttong = (Button) findViewById(R.id.buttong); r = new Random() buttong.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageView.setImageResource(images[r.nextInt(images.length)]); } });我不知道如何很好地解釋這一點(diǎn),但是例如:當(dāng)圖像“img250”出現(xiàn)時(shí),輸入類似“message.setText(“邁阿密海灘是佛羅里達(dá)州南部的一個(gè)島嶼城市”);”我試過 if/else 語句但沒有成功
1 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
根據(jù)您在評(píng)論中發(fā)布的 if 語句,看起來您在設(shè)置圖像時(shí)選擇了一個(gè)隨機(jī)圖像,在設(shè)置文本時(shí)選擇了另一個(gè)圖像。首先選擇圖像,然后使用它來設(shè)置圖像和文本。
例如:
Integer randomResource = images[r.nextInt(images.length)];
imageView.setImageResource(randomResource );
if (randomResource == R.drawable.img250) {
message.setText("Miami Beach is a south Florida island city");
} else {
// ...
}
添加回答
舉報(bào)
0/150
提交
取消