我想在屏幕上显示整个C程序应是可见的用户。
I wanted to show the whole c program on screen which should be visible to user.
我用的TextView,但我得到的错误为code含有特殊符号。
I used textView but i am getting errors as the code contains special symbols.
例如:
android:text=" #include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}" />
我也希望用户应当能够滚动code为codeS可能比example.I大是一个小白,请建议我所有备用的TextView的显示code。
I also want that the user should be able to scroll the code as the codes may be larger than the example.I am a noob so please suggest me any alternate for the textView to display the code.
保存C code。在资产文件夹中的文件,例如资源/资产/ codeC。
Save your C code in a file in assets folder, for example "res/assets/code.c".
编写一个函数读取一个文件到一个字符串的内容:
Write a function that reads the content of a file to a String:
private String readFileInAssetsDir(String filename) {
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(filename)));
String line;
while((line = br.readLine()) != null)
sb.append(line + "\n");
} catch(Exception e) {
// TODO
}
return sb.toString();
}
现在在布局定义的WebView(不是一个TextView)(其优点是可以显示任何字符,web视图提供了缩放和滚动直接):
And now define a WebView (not a TextView) in your layout (the advantages are that you can show any character, and WebView provides zoom and scroll directly):
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
最后,我会附上所有的C code在&LT; pre&GT;&LT; / pre&GT;
标记,然后在里面表现出来web视图控件:
And finally I would enclose all C code in a <pre></pre>
tag and then show it inside the WebView widget:
String plainCode = readFileInAssetsDir("code.c");
String htmlCode = "<pre>" + plainCode + "</pre>";
webView.loadDataWithBaseURL("", htmlCode, "text/html", "utf-8", "");