android java获取string.xml_android获取string.xml的值

[http://www.blogjava.net/zhip/archive/2011/01/25/343511.html共有属性:java代码中通过btn1关联次控件android:id=@+id/btn1控件宽度android:layout_width=80px    //80d

在android开发过程中,编写java代码中的常量过一般情况下,我们是定义在string.xml这个文件中。这样修改起来也很方便,而且做国际化也很简单。

这个string.xml的值会被R文件映射,所以可以看到R文件全是定义为int类型,就像是一个地址指引一样。

获取string.xml文件里面的值有几个不同的地方。

1. 在AndroidManifest.xml与layout等xml文件里:

view plain

copy to clipboard

print

?

android:text="@string/resource_name" android:text="@string/resource_name"

[开发过Android应用的同学们都知道,Android工程-res- layout资源文件夹下存放着控制view布局的xml文件,我们可以通过 getViewById(int i)方法,从XML中构造view及其子类,

2.在activity里:

view plain

copy to clipboard

print

?

方法一:this.getString(R.string.resource_name);

方法二:getResources().getString(R.string.resource_name); 方法一:this.getString(R.string.resource_name); 方法二:getResources().getString(R.string.resource_name);

3在其他java文件(必须有Context或pplication)

view plain

copy to clipboard

print

?

context.getString(R.string.resource_name);

application.getString(R.string.resource_name);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中读取Maven的pom.xml文件可以使用以下步骤: 1. 创建文件对象 首先需要创建一个File对象,用于表示pom.xml文件的路径。可以使用以下代码创建File对象: ``` File pomFile = new File("pom.xml"); ``` 2. 加载pom.xml文件 使用Java的DOM解析器来加载pom.xml文件。可以使用以下代码来加载pom.xml文件: ``` DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); ``` 3. 获取根元素 通过Document对象获取pom.xml文件的根元素。可以使用以下代码获取根元素: ``` Element root = doc.getDocumentElement(); ``` 4. 获取元素 通过Element对象获取pom.xml文件中的元素。可以使用以下代码获取元素: ``` String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); ``` 以上代码将获取pom.xml文件中的groupId、artifactId和version元素的。您可以根据需要获取其他元素的。 完整的代码示例: ``` import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class ReadPomXml { public static void main(String[] args) throws Exception { File pomFile = new File("pom.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); Element root = doc.getDocumentElement(); String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); System.out.println("groupId: " + groupId); System.out.println("artifactId: " + artifactId); System.out.println("version: " + version); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值