2013-06-04 55 views
0

我有一个ListView,每个项目在点击后返回相同的WebView类。我想用代码作为参数传递来填充HTML页面内的一些字段,以创建具有相同结构的不同内容。用数据填充HTML页面

我的网页上有这样的代码:

h2 {color: %@;} 
section#title {background-color: %@;} 
section#video a { color: %@; } 
section#formation a { color: %@; } 

<h1>%@</h1> 
<p class="type-metiers">%@</p> 

,所以我需要替换HTML文件的每个%@。我如何做一个修改外部文件内容的循环?

回答

0

将HTML中的文本放入字符串中,并将正则表达式应用于该字符串。

String s = html.toString(); 
String replaceWith = "Put something in this string"; 

source.replaceAll("%@", replaceWith); 

如果你想用不同的值来代替,你可以使用模式/匹配器

String s = html.toString(); 

Pattern p = Pattern.compile("%@"); 
Matcher m = p.matcher(s); 
while (m.find()) { 
    // Find each match in turn; 
    // Process your findings here; 
} 
+0

听起来不错,谢谢!但是我必须用同一页面内的不同值替换%@。 – kom

+1

添加了一个解决方案,以便您可以输入不同的值 – Flexo