android - 如何在 AndroidStudio 上设置已检查的 ImageView
问题描述
我正在使用 Texas Instruments 的 CC2640 和我的自定义应用程序。一切正常,但我发现了一个错误。在实践中,我可以从应用程序中读取设备输入的状态,因此如果它们是空闲的(绿灯),如果它们是忙碌的(红灯)。此时设置为 Settogglebutton,但我不想在布局中作为按钮,而是作为更改为 true/False 值的图像。我怎样才能做到这一点?这是我的部分代码:
if (intent.hasExtra(MainActivity.EXTRA_INPUT1)) {
setToggleButtonState(R.id.ingresso, intent.getIntExtra(MainActivity.EXTRA_INPUT1, 0));
} else if (intent.hasExtra(MainActivity.EXTRA_INPUT2)) {
setToggleButtonState(R.id.ingresso2, intent.getIntExtra(MainActivity.EXTRA_INPUT2, 0));
}
private void setToggleButtonState(int id, int value) {
if (id != -1) {
final ToggleButton b = (ToggleButton) findViewById(id);
if(value == 1){
b.setChecked(true);
}
else{
b.setChecked(false);
}
}
}
解决方案
让我看看我是否理解正确。您想根据值更改 ImageView 资源吗?
private void setImageResource(int id, int value) {
if (id != -1) {
final ImageView myImageView = (ImageView)findViewById(id); // i assume you wont use this imageview anywhere else, you just want to change the resource.
if(value == 1){
myImgView.setImageResource(R.drawable.true);
}
else{
myImgView.setImageResource(R.drawable.false);
}
}
如果您希望 imageview 保存该值,您可以将其定义为属性并将其保存在 image 标记中。
推荐阅读
- javascript - Web Speech API - SpeechSynthesisUtterance onmark 事件不会触发
- python - 如何删除文本文件中的字符串,Python?
- reactjs - 如何使用渲染解决方案重新创建代码
- javascript - 'Element | 类型的 rxjs 参数 null' 不可分配给 JQueryStyleEventEmitter 类型的参数
- cryptocurrency - 失败并出现错误 PancakeRouter: INSUFFICIENT_B_AMOUNT
- javascript - 如何在 React Js 中处理带有多个复选框的数据显示
- c++ - 错误:对象之前的预期类型说明符
- python - 获取 td 标签的值
- python - PyTorch 使用没有 For 循环的核函数生成矩阵
- arrays - 如何缩写 if 或嵌套公式