首页 > 解决方案 > 这是 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. 当开关未选中时,它将保持打印“1”并检查眩光

它将根据返回结果更新文本视图“”/“检测到眩光”。

  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 。检查开关时,它不会替换“----------”。

标签: javaandroid

解决方案


推荐阅读