Android-制作一个日志文件,并在中显示为文本视图,每个“文本”都有新行;事件;?
Hi-im在android中将姓名和号码记录到一个文件中, 然后我打开日志文件,在文本视图中显示。 但我无法添加“换行符”(“\n”),因此它显示: 名称1 nr1 姓名2 nr2 姓名3 nr3 姓名4 nr4 我只收到一条长长的队伍中的“一团乱” 我尝试以下方法: //我还尝试添加\n、\n、\\n,但没有设置“新行”-触发器 //你有什么想法吗:-) 编辑: 我只想说得更准确一些: 我现在有,但这不起作用:(: 结果: 名称1 nr1名称2 nr2名称3 nr3名称4 nr4 而不是: Name1 nr1 姓名2 nr2 姓名3 nr3 名称4 nr4 在XML中,我有:Android-制作一个日志文件,并在中显示为文本视图,每个“文本”都有新行;事件;?,android,textview,newline,logfile,Android,Textview,Newline,Logfile,Hi-im在android中将姓名和号码记录到一个文件中, 然后我打开日志文件,在文本视图中显示。 但我无法添加“换行符”(“\n”),因此它显示: 名称1 nr1 姓名2 nr2 姓名3 nr3 姓名4 nr4 我只收到一条长长的队伍中的“一团乱” 我尝试以下方法: //我还尝试添加\n、\n、\\n,但没有设置“新行”-触发器 //你有什么想法吗:-) 编辑: 我只想说得更准确一些: 我现在有,但这不起作用:(: 结果: 名称1 nr1名称2 nr2名称3 nr3名称4 nr4 而不是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingTop="20dp"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" >
<TextView
android:id="@+id/logg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/emty"
android:textSize="21sp"
android:maxLines="5"
android:scrollbars="vertical"/>
</LinearLayout>
然后:
String content = siteName + "_" + shortDate + "_" ;
content = content + System.getProperty("line.separator");
//(yes I know i probably want to use stringbuilder, or is it a must here?..)
然后将其发送到日志,3次,结果:
事件:_123_事件:_123_事件:123
而不是:
活动:123
活动:123
活动:123
试试这个
String content = Name + " " + Nr + " " + System.getProperty("line.separator") + " ";
经过大量的测试,我发现自己 像我以前那样在文件中添加新行是错误的:
String content = Name + " " + Nr + " " + System.getProperty("line.separator");
(然后将其保存到文件,然后读取..)
字符串和文件可能有“2个不同的&unice line.separator”:-
所以我的解决方案是:
String content = siteName + " " + shortDate + "//n" ;
//the "//n" is just a "MARKER" for later..
content = content.replaceAll(" ", " "); //just for nice things up..
//and save to file...
.....
//then when the textview want to display this:
// after the file have revived using getLogg()
loggfil = getLogg();
//then i look after the MARKER: "//n" and replace this..
loggfil = loggfil.replaceAll("//n", System.getProperty("line.separator"));
TextView tv = new TextView(this);
tv.setText(loggfil);
tv.setMovementMethod(new ScrollingMovementMethod());
setContentView(tv);
结果是:
存档:
名称1 Nr1//nName2 Nr2//nName3 Nr3//nName4 Nr4//n
在“保存的字符串loggfil”中
名称1 Nr1
姓名2 Nr2
姓名3 Nr3
姓名4 Nr4
我真高兴,终于!!!:-D
但是,仍然:
是否可以将“日志”写在“最新名称”的前面而不是最后一个?
我使用:
所以文件本身没有新行?确切地说:-(“Name1 nr1 Name2 nr2 Name3 nr3 Name4 nr4”您的文件是否有可能包含有空格的元素,如名称1 nr1名称2 nr2名称3 nr3?哇,有趣的是:testNAME 1 Nr 08 12:58:11 testNAME 2 Nr 08 12:59:16..而不是:testNAME 1 Nr 08 12:58:11 testNAME 2 Nr 08 12:59:16..我测试了使用:Name=“Event:”Nr=“123”强制字符串值;然后:String content=siteName+“”+shortDate+“”;content=content+System.getProperty(“line.separator”);然后将其发送到日志,2次,结果:事件:_123_事件:_123_事件:_123_事件:_123_+新行事件:_123_+新行注意:是否可以写入“日志”以便“最新的名字”是第一个,不是最后一个
String content = Name + " " + Nr + " " + System.getProperty("line.separator") + " ";
String content = Name + " " + Nr + " " + System.getProperty("line.separator");
String content = siteName + " " + shortDate + "//n" ;
//the "//n" is just a "MARKER" for later..
content = content.replaceAll(" ", " "); //just for nice things up..
//and save to file...
.....
//then when the textview want to display this:
// after the file have revived using getLogg()
loggfil = getLogg();
//then i look after the MARKER: "//n" and replace this..
loggfil = loggfil.replaceAll("//n", System.getProperty("line.separator"));
TextView tv = new TextView(this);
tv.setText(loggfil);
tv.setMovementMethod(new ScrollingMovementMethod());
setContentView(tv);
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("logg.txt",
Context.MODE_APPEND));
outputStreamWriter.write(content.toString());
//outputStreamWriter.write("test", 0, 4);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}