How to add string resources in Eclipse?

后端 未结 4 1234
Happy的楠姐
Happy的楠姐 2021-02-07 10:08

Can I have Eclipse adding my string resources as I code or do I have to switch to string.xml all the time and add each string?

4条回答
  •  遥遥无期
    2021-02-07 10:41

    Eclipse has wonderful time-saving shortcuts for this!

    1.- in XML editor:

    Say you have a Button,TextView, or any View with a hard-coded string as text:

    With the cursor over the string literal, press CTRL+1, then choose "Extract Android String". Choose the desired resource name (e.g. my_string_resource) then click OK. You will get a string resource in your strings.xml file:

    Text to add as string resource
    

    And your Button is now gonna look like:

    All automatically and without a single context-change :)

    2.- In Code editor:

    Write a string literal in your code like

    mButton.setText("Text to add as String resource");
    

    Then select the string literal (from " to "), and press CTRL+1, a yellow menu will appear, double click on "Extract Android String" (the S key does not work for me in this case, i just double click on the option). Choose the desired name (e.g. my_string_resouce), and click Ok. Again, you will get a new strings.xml entry:

    Text to add as string resource
    

    And your Button's setText line replaced by:

    mButton.setText(R.string.my_string_resource);
    

    Hope it helps and saves you as much time as it did for me! :)

提交回复
热议问题