我想说明整个C程序在屏幕上的android [英] I want to show the whole c program on screen in android

查看:98
本文介绍了我想说明整个C程序在屏幕上的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕上显示整个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", "");

这篇关于我想说明整个C程序在屏幕上的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆