java - 这是 setText 错误吗?
问题描述
问题
我的 TextView 设置为新字符串,但之后它将设置为以前的字符串。
布局
<TextView
android:id="@+id/qr_code_instruction"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text=" "
android:textColor="@color/qr_code_white"
app:layout_constraintBottom_toBottomOf="@+id/bottom2"
app:layout_constraintLeft_toLeftOf="@+id/left"
app:layout_constraintRight_toRightOf="@+id/right"
app:layout_constraintTop_toTopOf="@+id/bottom" />
切换监听器
glareSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
qr_code_instruction.setText("----------");
camera2Manager.setGlareSwitch(true); //turn off glare
}
else{
camera2Manager.setGlareSwitch(false); //turn on glare
}
}
});
检查类部分
if(!glareSwitch){
System.out.println("1");
glare = helper.glareDetection(bitmap);
if(glare){
((TextView) activity.findViewById(R.id.qr_code_instruction)).setText(" ");
} else {
((TextView) activity.findViewById(R.id.qr_code_instruction)).setText("Glare Detected");
}
}
else{
System.out.println("2");
glare = true;
}
漏洞
- 当开关未选中时,它将保持打印“1”并检查眩光
它将根据返回结果更新文本视图“”/“检测到眩光”。
- 当开关被选中时,它将保持打印 2 并且什么也不做。
它会将文本视图更新为“----------”,然后将其设置为以前的最新文本“”/“检测到眩光”。
开关点击的输出
ctrl + shift + f
更新
if(!glareSwitch){
System.out.println("1");
glare = helper.glareDetection(bitmap);
// if(glare){
// ((TextView) activity.findViewById(R.id.qr_code_instruction)).setText(" ");
// } else {
// ((TextView) activity.findViewById(R.id.qr_code_instruction)).setText("Glare Detected");
// }
}
else{
System.out.println("2");
glare = true;
}
通过注释掉类中的 setText 。检查开关时,它不会替换“----------”。
解决方案
推荐阅读
- java - Spring在一个类中拦截异常
- reactjs - reactjs阿波罗反应取消请求
- snmp - 在 4.4.12 中使用 resolveWithMib 面临问题“TypeError:resolveWithMib() 恰好需要 2 个参数(3 个给定)”。它在 4.4.4 中工作
- java - 如何从 firebase 实时数据库中检索两个不同的值并在 onDataChange 方法之外使用?
- python - 处理内存中的大量文本数据
- php - 防止 div 从表 th 中的新行开始
- php - Carbon createFromFormat 错误 -> 数据丢失
- java - 为什么 AbstractOwnableSynchronizer.exclusiveOwnerThread 未声明为 volatile?
- javascript - 在下拉列表中控制月份年份
- sapui5 - 智能表扩展到 2 个实体集?