我是 java 和 android 开发的新手 我正在开发一个包含简单 GUI 的应用程序,用于为学生访问和阅读 C 和 Java 中的基本程序。
我正在尝试用一个文本文件(在本例中是一个 C 程序)填充一个 webview,该文本文件预加载到我的数据库的某个列中。
我决定不使用 ScrollView ,因为同时滚动和缩放很难实现。缩放需要自定义实现,这对我来说很难理解。
因此我决定使用 webview,因为它很容易滚动并且只需要 1 行代码来实现文本缩放。
我有两个问题:
1) webview 无法使用我当前的实现处理换行符。即它会尝试在同一行显示所有文本,而不是转到下一行
2) 它不显示“stdio.h”、“stdlib.h”等....基本上所有带有 .h 扩展名的词都是几乎所有 C 程序的一部分。显示是必不可少的这些。
我尝试了以下 2 种实现(可能不正确):
1)loadData(字符串数据,字符串mimeType,字符串编码)
2) loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
我的代码如下:
package com.example.uopengineering;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.webkit.WebView;
public class viewProgramActivity<x> extends Activity {
private SQLiteDatabase database;
ExternalDbOpenHelper dbOpenHelper;
private WebView webView;
private static final String DB_NAME = "umangdb.db";
private String rowid;
private String programtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewprogram);
dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
Intent intent=getIntent();
rowid=intent.getStringExtra("myextra");
String query="select * from engg_table where _id="+rowid;
Cursor myCursor = database.rawQuery(query,null);
myCursor.moveToFirst();
programtext=myCursor.getString(4);
myCursor.close();
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
//webView.loadData(programtext, "text/html", "UTF-8");
try {
webView.loadData(URLEncoder.encode(programtext,"utf-8").replaceAll("\\+"," "), "text/html", "utf-8");
//webView.loadDataWithBaseURL(null, programtext, null, query, null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相应的 XML 文件(用于布局目的)是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines = "22"
android:scrollbars = "vertical"
/>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
我浏览了其他有类似问题的论坛,但找不到合适的解决方案。 其中一位建议使用 javascript 解决此问题。 我所知道的关于 javascripts 的是在我的大学项目的网络申请表中进行验证。这可能吗??如果是这样,您建议我如何着手学习 javascript 以及我应该如何实现它。
此外,replaceAll("\+","") 是如何工作的?它究竟是如何删除不需要的字符的?另外我对字符编码标准不是很熟悉
最佳答案
仅将内容类型更改为“text/plain”在 Android 4.4 中对我不起作用。所有内容仍然在一行中。 不过,这个 Stack Overflow 问题中接受的答案是有效的:
Android WebView incorrectly handling newlines in preformatted text
关于javascript - 如何使用数据库中的纯文本填充 android 中的 webview 并能够处理换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248065/