1
我的應用程序使用webview並在資產/文件夾中加載本地html文件。 現在有沒有辦法在res/strings.xml中使用該html文件webview加載的所有字符串?在webview中使用來自strings.xml的值
嘗試了一些和谷歌整夜,但havnt得到任何解決方案,它甚至有可能嗎?
我的應用程序使用webview並在資產/文件夾中加載本地html文件。 現在有沒有辦法在res/strings.xml中使用該html文件webview加載的所有字符串?在webview中使用來自strings.xml的值
嘗試了一些和谷歌整夜,但havnt得到任何解決方案,它甚至有可能嗎?
像@ doc_180所說,使用javascript將是一個不錯的選擇。這個example可能是您注入字符串的好方法。
不過,當然,你可以做一些事情變得更容易,例如,要傳遞到的WebView的HTML是:
<body>
<h1>$REPLACE_THIS</h1>
...
然後,你可以簡單地做字符串替換/正則表達式替換您追加HTML之前webview爲您自己的注入字符串。
E.g.
String html = ... // Any method to load the html file into string
html = html.replace("$REPLACE_THIS", getResouces().getString(R.string.replacement));
webview.loadData(html, "text/html", "utf-8");
更新
原來他需要動態是指在R.string的常量字符串;這是如何做到的:
public String getStringFromRes(String name){
try{
int resId = R.string.class.getField(name).get(null);
return getResources().getString(resId);
}catch(Exception e){
// no such string
return "empty";
}
}
你是什麼意思?顯示網頁視圖的價值?如果是這樣,只需使用Resource獲取該字符串並將其作爲內容傳遞給Web視圖即可。 – xandy 2011-02-06 03:40:50
你想如何使用字符串? Webview和Android對象可以使用JavaScript進行對話。 (演示可在android示例中使用)。例如,我可以讓按鈕單擊顯示一條警告消息,我從android傳遞(存儲在string.xml中)。 – 2011-02-06 03:47:40