Android 将类中的文字提取到String.xml中

感谢这位国外的网站:http://envyandroid.com/extract-strings-from-code-to-strings-xml/

大意就是:使用快捷键shift+alt+a,选择右下角的android string就可以弹出对话框,取一个名字就可以自动在string.xml中。如下原文:

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  ADTAndroid DevelopmentEclipseI18nRefactoringStrings.XmlTipsXML

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 Text</string>
</resources>  

If you need to edit your text, just edit it in the strings.xml file.

Please share this tip.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值