根据 TextView 的更新更改背景

Changing Background based on update of TextView

我正在尝试使用 JAVA(Android Studio 创建一个简单的 android 应用程序。我愿意实现的是根据 TextView 的内容更改应用程序的背景颜色(它在两个值“打开”和“关闭”之间不断更新)。我已经看到必须使用 onChanged() 但我真的不明白如何为 TextView 实现侦听器。

我试过这样写: measurementValue = (TextView) findViewById(R.id.textbox_main); measurementValue.addTextChangedListener(new TextWatcher() {} 根据我在网上找到的内容,但无法真正弄清楚下一步是什么。我是一个新手所以请善待。

此致!

您可以在这 3 个回调中监听文本更改。这取决于您的用例。

@Override
public void onTextChanged(CharSequence textInput, int start, int before, int count) {
    String strInput = textInput.toString();

    if ("open".equalsIgnoreCase(strInput)) {
        // text is "open"
        // change background to desired color
    } else if ("closed".equalsIgnoreCase(strInput)) {
       // text is "closed"
       // change background to desired color
    }
}