Android(安卓)将类中的文字提取到String.xml中
感谢这位国外的网站: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 Textstring>resources>
If you need to edit your text, just edit it in the strings.xml file.
Please share this tip.
SUBSCRIBEHow 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 Textstring>resources>
If you need to edit your text, just edit it in the strings.xml file.
Please share this tip.
更多相关文章
- android中给用户提醒的三种方式
- Android(安卓)AlertDialog对话框详解及实例
- AndroidGUI14:各种Dialog常用技巧
- anroid studio更新中出现The Android(安卓)SDK folder can no lo
- 【简单的学生管理界面】Android添加简单的日历控件
- 【Android(安卓)开发教程】设置Activity的样式和主题
- android 组件之 alertDialog
- android对话框之AlertDialog.Builder类的setSingleChoiceItems
- Android(安卓)stuido 快捷键