android - 在 ImageButton 上设置前景和背景图像并通过 ID 访问它们
问题描述
我想创建一个按钮。在 XML 代码中选择了谁的默认图像
现在,当活动开始时,我使用给定的 JAVA 代码从按钮背景中的多个图像中设置随机图像
我的问题是我想在用户单击按钮时显示背景图像(与使用下面的 JAVA 代码随机设置的相同)。如何通过 id 访问背景图像和通过 id 访问前景图像,以及 onClick 事件我显示背景图像并隐藏前景?如果可以怎么办????
感谢帮助!
android:id="@+id/imageButton_0"
android:tag="imageButton_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/emoji" />
// JAVA code
int randomPosition = new Random().nextInt(positionArray.length - count);
String str = "imageButton_" + Integer.toString(positionArray[randomPosition]);
Log.v(TAG, str);
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.my_game_board);
ImageButton btn = constraintLayout.findViewWithTag(str);
btn.setBackgroundResource(imagesArray[i]);```
解决方案
推荐阅读
- swift - SwiftUI - 日期格式(前导零)
- html - HTML 禁用浏览器缩放
- http - 为什么 Esp8266 HTTP 客户端无法接收数据?
- azure - 在 Salesforce 中使用 Azure SSO 详细信息对 Azure REST API 请求进行身份验证
- kotlin - 扩展属性中的 var 和 val 有什么区别?
- apache-kafka - java中自动重启Kafka Stream
- r - 生成过滤后的 SHINY 网页的 HTML 链接
- java - Docker:Springboot 容器无法连接到 PostgreSql 容器
- mulesoft - 结合骡子表达语言和文字
- r - 在 R 中动态创建和评估函数