androidhtmlstring
『壹』 android 怎麼改變html中文字的大小
通過android代碼與html代碼交互實現。下面是交互的小例子,這個例子來源於android學習手冊,360手機助手中下載,包含108個例子,文檔還有源碼。
1 調用網頁上的js代碼
Android中可以通過webview來實現和js的交互,在程序中調用js代碼,只需要將webview控制項的支持js的屬性設置為true,,然後通過loadUrl就可以直接進行調用,如下所示:
mWebView.getSettings().setjavaScriptEnabled(true);
mWebView.loadUrl("javascript:test()");
2 網頁java代碼的方法
在網頁中調用java代碼,需要在webview控制項中添加javascriptInterface。如下所示:
mWebView.addJavascriptInterface(newObject() {
publicvoid clickOnAndroid() {
mHandler.post(newRunnable() {
publicvoid run() {
Toast.makeText(Test.this,"測試調用java", Toast.LENGTH_LONG).show();
}
});
}
},"demo");
在網頁中,只需要像調用js方法一樣,進行調用就可以
<div id='b'><a onclick="window.demo.clickOnAndroid()">b.c</a></div>
3 Java代碼調用js並傳參
首先需要帶參數的js函數,如functiontest(str),然後只需在調用js時傳入參數即可,如下所示:
mWebView.loadUrl("javascript:test('aa')");
4.Js中調用java函數並傳參
首先一樣需要帶參數的函數形式,但需注意此處的參數需要final類型,即得到以後不可修改,如果需要修改其中的值,可以先設置中間變數,然後進行修改。如下所示:
mWebView.addJavascriptInterface(newObject() {
publicvoid clickOnAndroid(final int i) {
mHandler.post(newRunnable() {
publicvoid run() {
intj = i;
j++;
Toast.makeText(Test.this,"測試調用java" + String.valueOf(j), Toast.LENGTH_LONG).show();
}
});
}
},"demo");
『貳』 Android 在strings.xml中定義html標簽
第二種情況 Android在通過Context.getString獲取字元串時做了處理,把Html標記都去掉了,這個可以把斷點跟蹤一下。所以第二種情況沒有傾斜加粗的效果。
第二種情況這么修改就有效果了:
textView.setText(this.getText(R.string.tagged_string));
這也說明了Context.getText 和 Context.getString的區別。
『叄』 請問如何用android解析html格式的字元串,並顯示在一般控制項中
private void loadHTML(){
String html = "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <title></title>\n" +
"</head>\n" +
"<body>\n" +
" Hello!耶耶耶<br/>\n" +
"HTML Page here!\n" +
"<img src=\"img/bdlogo.gif\"></im>\n" +
"</body>\n" +
"</html>";
webView.loadData(html, "text/html", "UTF-8");
// webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
}
『肆』 android中如何在textview中加入html
具體代碼如下:
Android中的TextView,本身就支持部分的Html格式標簽。這其中包括常用的字體大小顏色設置,文本鏈接等。使用起來也比較方便,只需要使用Html類轉換一下即可。比如:
textView.setText(Html.fromHtml(str));
然而,有一種場合,默認支持的標簽可能不夠用。比如,我們需要在textView中點擊某種鏈接,返回到應用中的某個界面,而不僅僅是網路連接,如何實現?
經過幾個小時對android中的Html類源代碼的研究,找到了解決辦法,並且測試通過。
『伍』 android如何保存html文件,包括其中的圖片。
1、先示例圖片
2、操作代碼
package com.nekocode.xue.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.nekocode.xue.PublicData;
import com.nekocode.xue.PublicData.Subscribe;
public class HtmlStorageHelper {
private String URL = "http://eproject.sinaapp.com/fetchurl.php/getcontent/";
private PublicData pd;
private AQuery aq;
private SQLiteDatabase mDB;
private String mDownloadPath;
public HtmlStorageHelper(Context context) {
pd = PublicData.getInstance();
aq = new AQuery(context);
mDB = context.openOrCreateDatabase("data.db", Context.MODE_PRIVATE, null);
mDB.execSQL("create table if not exists download_html(_id INTEGER PRIMARY KEY AUTOINCREMENT, content_id TEXT NOT NULL, title TEXT NOT NULL)");
mDownloadPath = pd.mAppPath + "download/";
File dir_file = new File(pd.mAppPath + "download/");
if(!dir_file.exists())
dir_file.mkdir();
}
public void saveHtml(final String id, final String title) {
if(isHtmlSaved(id))
return;
aq.ajax(URL+id, String.class, new AjaxCallback<String>() {
@Override
public void callback(String url, String html, AjaxStatus status) {
File dir_file = new File(mDownloadPath + id);
if(!dir_file.exists())
dir_file.mkdir();
Pattern pattern = Pattern.compile("(?<=src=")[^"]+(?=")");
Matcher matcher = pattern.matcher(html);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
downloadPic(id, matcher.group(0));
matcher.appendReplacement(sb, formatPath(matcher.group(0)));
}
matcher.appendTail(sb);
html = sb.toString();
writeHtml(id, title, html);
}
});
}
private void downloadPic(String id, String url) {
File pic_file = new File(mDownloadPath + id + "/" + formatPath(url));
aq.download(url, pic_file, new AjaxCallback<File>() {
@Override
public void callback(String url, final File file, AjaxStatus status) {
}
});
}
private void writeHtml(String id, String title, String html) {
File html_file = new File(mDownloadPath + id + "/index.html");
FileOutputStream fos = null;
try {
fos=new FileOutputStream(html_file);
fos.write(html.getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
ContentValues values = new ContentValues();
values.put("content_id", id);
values.put("title", title);
mDB.insert("download_html", "_id", values);
}
public boolean isHtmlSaved(String id) {
File file = new File(mDownloadPath + id);
if(file.exists()) {
file = new File(mDownloadPath + id + "/index.html");
if(file.exists())
return true;
}
deleteHtml(id);
return false;
}
public String getTitle(String id) {
Cursor c = mDB.rawQuery("select * from download_html where content_id=?", new String[]{id});
if(c.getCount() == 0)
return null;
c.moveToFirst();
int index1 = c.getColumnIndex("title");
return c.getString(index1);
}
public ArrayList<Subscribe> getHtmlList() {
Cursor c = mDB.rawQuery("select * from download_html", null);
ArrayList<Subscribe> list = new ArrayList<Subscribe>();
if(c.getCount() != 0) {
c.moveToFirst();
int index1 = c.getColumnIndex("content_id");
int index2 = c.getColumnIndex("title");
while (!c.isAfterLast()) {
String id = c.getString(index1);
if(isHtmlSaved(id)) {
Subscribe sub = new Subscribe(
id,
c.getString(index2),
Subscribe.FILE_DOWNLOADED
);
list.add(sub);
}
c.moveToNext();
}
}
return list;
}
public void deleteHtml(String id) {
mDB.delete("download_html", "content_id=?", new String[]{id});
File dir_file = new File(mDownloadPath + id);
deleteFile(dir_file);
}
private void deleteFile(File file) {
if (file.exists()) { // 判斷文件是否存在
if (file.isFile()) { // 判斷是否是文件
file.delete(); // delete()方法 你應該知道 是刪除的意思;
} else if (file.isDirectory()) { // 否則如果它是一個目錄
File files[] = file.listFiles(); // 聲明目錄下所有的文件 files[];
for (int i = 0; i < files.length; i++) { // 遍歷目錄下所有的文件
this.deleteFile(files[i]); // 把每個文件 用這個方法進行迭代
}
}
file.delete();
} else {
//
}
}
private String formatPath(String path) {
if (path != null && path.length() > 0) {
path = path.replace("\", "_");
path = path.replace("/", "_");
path = path.replace(":", "_");
path = path.replace("*", "_");
path = path.replace("?", "_");
path = path.replace(""", "_");
path = path.replace("<", "_");
path = path.replace("|", "_");
path = path.replace(">", "_");
}
return path;
}
}
3、這段代碼簡單修改就可以用到自己的項目中了