我需要使用sax解析器在android上获取多个按钮的名称,并将其保存在一个文件中,在某些情况下,如果我做了任何应该设置(加载)到按钮文本的更改。例如:
Button1文本是:生活是好的;Button2文本是:生活不是好的。
"Button1,生活是好的;Button2,生活是不好的“应该保存在log.txt中。如果我在那个log.txt文件中做任何修改,比如(可以更新):"Button1,生活相当好;Button2,生活不是幸福“,比
Button1文本是:生活相当好;Button2文本是:生活不是幸福的
那么,我该怎么做呢,谢谢。
发布于 2016-05-19 19:56:39
有一种更简单的方法可以做到这一点:您可以使用字符串资源。String Resources \ Android开发人员
<resources>
<string name="life_is_x">Life is %s</string>
<string name="good">good</string>
<string name="not_good">not good</string>
<string name="quite_good">quite good</string>
</resources>在布局XML中,您不需要在按钮上放置属性android:text的值(尽管如果指定tools:text,该值将仅显示在Android图形布局设计器中,而不显示在应用程序中,这对设计非常有用)。
然后在您的代码中:
Context context = button.getContext();
String lifeStr;
switch (lifeEnum) {
case GOOD:
lifeStr = context.getString(R.string.good);
break;
case NOT_GOOD:
lifeStr = context.getString(R.string.not_good);
break;
case QUITE_GOOD:
lifeStr = context.getString(R.string.quite_good);
break;
}
String title = context.getString(R.string.life_is_x, lifeStr);
// lifeStr will replace the %s placeholder in life_is_x
button.setText(title);发布于 2016-06-07 06:18:38
我已经解决了jdom解析器的问题,实际上我使用这个站点:document.htm也使用这个链接来创建xml文件:document.htm。谢谢你给了我主意。我建议任何想在本地安卓设备上玩xml操作的人都应该看看xml/。您可能会发现任何类型的xml示例。
<?xml version="1.0"?>
<class>
<student rollno="393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>jasvir</firstname>
<lastname>singn</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class JDomParserDemo {
public static void main(String[] args) {
try {
File inputFile = new File("input.txt");
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(inputFile);
System.out.println("Root element :"
+ document.getRootElement().getName());
Element classElement = document.getRootElement();
List<Element> studentList = classElement.getChildren();
System.out.println("----------------------------");
for (int temp = 0; temp < studentList.size(); temp++) {
Element student = studentList.get(temp);
System.out.println("\nCurrent Element :"
+ student.getName());
Attribute attribute = student.getAttribute("rollno");
System.out.println("Student roll no : "
+ attribute.getValue() );
System.out.println("First Name : " + student.getChild("firstname").getText());
System.out.println("Last Name : "+ student.getChild("lastname").getText());
System.out.println("Nick Name : "+ student.getChild("nickname").getText());
System.out.println("Marks : "+ student.getChild("marks").getText());
}
}catch(JDOMException e){
e.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}https://stackoverflow.com/questions/37331171
复制相似问题