问题描述
我想使用TextView
在我的应用程序中记录一些操作.以下方法是不好的做法吗(有什么缺点吗?)
I'd like to use a TextView
to log some actions in my app. Is the following method bad practice (are there any drawbacks?)
TextView tv = (TextView)findViewById(R.id.textView);
String s = "";
s = s + "Starting app...\n";
tv.setText(s);
...
s = s + "Doing action #1.\n";
tv.setText(s);
...
s = s + "Doing action #2.\n";
tv.setText(s);
在将新的日志记录信息附加到s
之后,是否有比每次重做setText(s)
更好的方法?
Is there a better way to do it than redo a setText(s)
each time, after having appended the new logging information to s
?
推荐答案
有一种非常干净的方法,那就是在android中使用DataBinding
.我将通过一个简短的示例来说明您的用例最终会如何显示.
There's a pretty clean approach, which is to use DataBinding
in android. I will go through a short example of how your use case might end up looking.
首先,要使用DataBinding
,您必须在应用的build.gradle中启用它:
First, to use DataBinding
you must enable it from the build.gradle of your app:
android {
dataBinding {
enabled = true
}
}
第二件事是创建一个包含您的日志消息的模型,这也是一种存储消息的简单方法,而不是简单地附加到字符串上.就我而言,我称其为Log
:
Second thing would be to create a model that would contain your log message, this is also a clean way to store the message instead of simply appending to a string. In my case, I'll call it Log
:
public class Log extends BaseObservable{
//This will be the message you wanna print
private String message;
@Bindable
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
//This would automatically update any binded views with this model whenever the message changes
notifyPropertyChanged(BR.message);
}
}
第三件事是更新包含正在使用的TextView
的布局文件.基本上,我们将在布局中创建一个Log
变量,并告诉textView
从其中读取消息并将其显示为文本.在我的示例中,activity_main.xml
:
Third thing would be to update your layout file which contains the TextView
you're using. Basically, we'll create a Log
variable in the layout, and tell the textView
to read the message from it and display it as its text. In my example, activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="log"
type="com.riad.crypto.databinding.Log" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:textSize="18sp"
android:text="@={log.message}" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_view"
android:gravity="bottom"/>
</LinearLayout>
现在,简单地,无论您要显示日志的何处,都创建一个Logger
对象并将其与textView
绑定.在我的示例中,MainActivity
:
Now simply, wherever you wanna display the logs, create a Logger
object and bind it with the textView
. In my example MainActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Log logger = new Log();
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setLog(logger); //This is where we bind the layout with the object
Button button = (Button) findViewById(R.id.button_view);
logger.setMessage("1st log statement"); //You'll notice that the texview displays this message first
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logger.setMessage("2nd log message"); //After button click, the texview would automatically change text to this. All you did was set the message
}
});
}
因此,现在您可以在每次单击按钮时更改textView
,但是当然您可以在任意位置进行更改.如果在不同的类中需要Log
对象,则可以通过创建Singleton
之类的东西来改进它,但这只是用于演示.
So now you could change the textView
on every button click, but ofcourse you could do it wherever you want. And in case you need the Log
object in different classes you could improve this by creating a Singleton
or something, but this was just for the demo.
希望这会有所帮助!祝你好运
Hope this helps! Goodluck
这篇关于在TextView中记录信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!