Android 将类中的文字提取到String.xml中
来源:互联网 发布:windows正版镜像 编辑:程序博客网 时间:2024/06/08 07:25
感谢这位国外的网站:http://envyandroid.com/extract-strings-from-code-to-strings-xml/
大意就是:使用快捷键shift+alt+a,选择右下角的android string就可以弹出对话框,取一个名字就可以自动在string.xml中。如下原文:
ittle feature can save a lot of time and manual work when working with Android apps.
If you during Android development in Eclipse use hardcoded strings in your java code, like in the example below:
package test.layout;import android.app.Activity; import android.os.Bundle; import android.widget.Button;public class LayoutTest extends Activity { private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.Button01); btn.setText("New Button Text"); }}
Maybe you want to gather them in the strings.xml file where they belong, to separate hardcoded strings from the code and to get an overview of your string resources.
This also makes it easier to internationalize your application later on.
You can just select the string to extract out, “New Button Text” on line 17, and then in theEclipse main menu, go to
Refactor -> Android -> Extract Android String…
You can then give your new string a name, edit the string text if you want, and select where to place it (the default is in you strings.xml file: /res/values/strings.xml).
Then hit the OK button to extract your string.
Tip: You could also use the keyboard shortcut: First press Shift + Alt + A, then press S
You will notice that the hardcoded string “New Button Text” gets replaced with a resource id, R.string.new_text:
btn.setText(R.string.new_text);
This resource makes it easy to refer to your string several places in your code, while keeping only one version of the text itself in the strings.xml file, which now looks like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="new_text">New Button Text</string></resources>
If you need to edit your text, just edit it in the strings.xml file.
Please share this tip.
How to easily extract strings from your Android code into the strings.xml file
22 December 2010 - tagged ADT, Android Development, Eclipse, I18n, Refactoring, Strings.Xml, Tips, XMLThis handy little feature can save a lot of time and manual work when working with Android apps.
If you during Android development in Eclipse use hardcoded strings in your java code, like in the example below:
package test.layout;import android.app.Activity; import android.os.Bundle; import android.widget.Button;public class LayoutTest extends Activity { private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.Button01); btn.setText("New Button Text"); }}
Maybe you want to gather them in the strings.xml file where they belong, to separate hardcoded strings from the code and to get an overview of your string resources.
This also makes it easier to internationalize your application later on.
You can just select the string to extract out, “New Button Text” on line 17, and then in theEclipse main menu, go to
Refactor -> Android -> Extract Android String…
You can then give your new string a name, edit the string text if you want, and select where to place it (the default is in you strings.xml file: /res/values/strings.xml).
Then hit the OK button to extract your string.
Tip: You could also use the keyboard shortcut: First press Shift + Alt + A, then press S
You will notice that the hardcoded string “New Button Text” gets replaced with a resource id, R.string.new_text:
btn.setText(R.string.new_text);
This resource makes it easy to refer to your string several places in your code, while keeping only one version of the text itself in the strings.xml file, which now looks like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="new_text">New Button Text</string></resources>
If you need to edit your text, just edit it in the strings.xml file.
Please share this tip.
- Android 将类中的文字提取到String.xml中
- Android Studio 将字符串快速生成到string.xml中
- android 文字引用排版 String.xml中文字引用格式
- android 替换string.xml中文字的方法
- Android 中String.xml文件中的错误
- Android中string.xml中的替换
- Android中string.xml中的替换
- Android中string.xml中的替换
- Android中string.xml中的替换
- android中string.xml中的特殊符号写法
- android Studio将String.xml中的字段导出。
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- 【Android基础】textview改变一部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- android textview改变部分文字的颜色和string.xml中文字的替换
- 【Accelerated C++】重点回顾(续)
- mobiscroll插件使用
- GPG key generation: Not enough random bytes available.
- Jetty使用固定长度队列,导致无法服务
- 逻辑——真与假的世界
- Android 将类中的文字提取到String.xml中
- springmvc配置
- 实际上Java访问数据库的方式主要有四种
- codeforces 581d
- android学习之选择媒体库中的图片
- jfinal 学习笔记
- Android学习之Animation(三)
- 执行django-admin.py时,打开编辑器的解决办法
- 【ASP】利用数据库操作类优化数据库表的增删改查