这是我的主班。当静态文本在另一个类中更改时,请更改textview settext。注意:此过程将应用于多个视图
public class MainActivity extends AppCompatActivity {
public static String text = "123";
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.txt);
txt.setText(text);
}
}
另一堂课
public class AnotherClass{
public void changeTextViewText(){
MainActivity.text = "bulut";
}
答案 0 :(得分:2)
更改字符串时,应更新textview。在您的情况下,观察者模式是完成此任务的良好设计模式。基本方法:
//create a class that stores object.
public class Observer{
private static Observer instance = new Observer();
private HashMap<String,TextView> map;
public static Observer getInstance(){
return instance;
}
private Observer(){
map = new HashMap<>();
}
public static void subscribe(String viewKey, TextView view){
Observer observer = getInstance();
observer.map.put(viewKey, view);
}
public static void changeText(String viewKey, String text){
Observer observer = getInstance();
if(observer.map.containsKey(viewKey)){
TextView textView = observer.map.get(viewKey);
textView.setText(text);
}else{
// throw exception
}
}
// in your activity do this
Observer.subscribe("Main text view", txt);
// in your other classes simply do
Observer.changeText("Main text view", "bulut");
当然,您也可以使用某些库来完成此操作,例如https://github.com/ReactiveX/RxAndroid。或将textview作为参数当然发送到其他班级
答案 1 :(得分:1)
不建议在Activity
类中使用static,因为它可能导致内存泄漏。但是,如果必须,请在TextView
中而不是在String
中创建静态变量。像这样:
public class MainActivity extends AppCompatActivity {
public static String text = "123";
Button btn;
public static TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.txt);
txt.setText(text);
}
}
然后在另一堂课
public class AnotherClass{
public void changeTextViewText(){
MainActivity.txt.setText("bulut");
}
推荐方式
在另一个类中创建一个构造函数,如:
public class AnotherClass{
MainActivity mainActivity;
public AnotherClass(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
public void changeTextViewText() {
mainActivity.getTxt().setText("bulut");
}
}
在您的MainActivity中: 公共类MainActivity扩展了AppCompatActivity {
public static String text = "123";
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.txt);
txt.setText(text);
}
public TextView getTxt() {
return txt;
}
}
答案 2 :(得分:0)
防止内存泄漏并且不引用上下文(活动)
接口回调方法
更好的方法是:
建立界面:
var data = [{
x: [time],
y: [rand()],
mode:'lines',
fill:'tozeroy',
fillcolor :'rgba(249, 180, 14, 0.5)
}];
并在您的Activity上实现它,或创建它的实例
示例:
实施方法:
interface TextUpdater{
void updateText(String text);
}
实例方法:
MainActivity extends... implements TextUpdater{
...
..
@overRide
void updateText(String text){
textView.setText(text);
}
最后通过构造函数或setter方法将接口实例添加到另一个类中。
TextUpdater textUpdater = new TextUpdater(){
@overRide
void updateText(String text){
textView.setText(text);
}
}
在您的其他课程中,请引用接口:
ex1: otherClass.setUpdater(this) // (implementation case)
ex2: otherClass otherclass = new OtherClass(this)
// if you make a instance of interface then use it (change 'this')
然后调用
之类的界面 OtherClass {
TextUpdater textUpdater...
add constructor or do setter...
注意: 确保您在UI线程上运行
LiveData方法:(更好的选择)
textUpdater.updateText("Your String")
主要活动...
OtherClass .. {
MutableLiveData<String> textLiveData = new MutableLiveData()
...
void changeText(String text){
textLiveData.setValue(text)
}
更新您的文字,例如:
otherClass.getTextLiveData().observe(this,Observer<String>() {
@Override
public void onChanged(final String text) {
textView.setText(text)
}
)}