-1

ユーザーに表示されるはずのcプログラム全体を画面に表示したかったのです。

textView を使用しましたが、コードに特殊な記号が含まれているため、エラーが発生します。

例えば:

 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;
 }" />

また、コードが例よりも大きい可能性があるため、ユーザーがコードをスクロールできるようにしたいと考えています。

4

2 に答える 2

1

Cコードをassetsフォルダー内のファイル(「res / asset / code.c」など)に保存します。

ファイルの内容を文字列に読み取る関数を記述します。

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ではない)を定義します(利点は、任意の文字を表示できることと、WebViewがズームとスクロールを直接提供することです)。

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" />

そして最後に、すべてのCコードを<pre></pre>タグで囲み、WebViewウィジェット内に表示します。

    String plainCode = readFileInAssetsDir("code.c");
    String htmlCode = "<pre>" + plainCode + "</pre>";
    webView.loadDataWithBaseURL("", htmlCode, "text/html", "utf-8", "");
于 2013-01-15T14:47:19.073 に答える