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

Android 将类中的文字提取到String.xml中_第1张图片

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:

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

Android 将类中的文字提取到String.xml中_第2张图片

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:

  
<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)