我刚刚开始使用 RxJava/RxAndroid,我想知道是否可以使用它来解决以下问题。基本上,给定一个字段,比如一个文本视图和一个值,一个字符串,我正在寻找一种在字符串值发生变化时自动更新文本视图的方法。我不确定我将如何将其实现为 Observable。让我演示一下;
String str = "Test"; //the string value
TextView textView = (TextView) findViewById(R.id.textView); //the textview
Observable o = //looking for this part. Want to observe the String str
o.subscribe(new Observer<String>() { //subscribe here looking for string changes
@Override
public void onCompleted() {
System.out.println("Completed");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
textView.setText(s); //update the textview here
}
});
//here is where the string changes, it could be hardcoded, user input, or
//anything else really, I just want the textview to be updated automatically
//without another setText
str = "Different String";
我正在寻找的 RxAndroid/RxJava 是否可行?