感谢这位国外的网站: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.

SUBSCRIBE

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,  XML

This 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.

更多相关文章

  1. android中给用户提醒的三种方式
  2. Android(安卓)AlertDialog对话框详解及实例
  3. AndroidGUI14:各种Dialog常用技巧
  4. anroid studio更新中出现The Android(安卓)SDK folder can no lo
  5. 【简单的学生管理界面】Android添加简单的日历控件
  6. 【Android(安卓)开发教程】设置Activity的样式和主题
  7. android 组件之 alertDialog
  8. android对话框之AlertDialog.Builder类的setSingleChoiceItems
  9. Android(安卓)stuido 快捷键

随机推荐

  1. Flutter之旅:入门
  2. android客户端首次进入轮播引导图(一)
  3. Android SDK Manager安装Platform-tools
  4. 一个关于android中ListView的子控件中按
  5. Android Thread Looper Handler 关系
  6. Android TextView 中文本横向滚动效果实
  7. Android中使用AlarmManager需注意设置进
  8. android Uri的常用几个例子
  9. Android Studio MQTT功能
  10. android 6.0 权限问题的解决