How to add string resources in Eclipse?

后端 未结 4 1237
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:40

    The best practice is too have strings.xml inside values folder which keeps all string constants. Because later on if you want to make any change, it will easy for u if u keep in strings.xml. Otherwise you will have to always remember the file where u have wrote that constant.

    0 讨论(0)
  • 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:

    <Button    
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Text to add as string resource"/>
    

    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:

    <string name="my_string_resource">Text to add as string resource</string>
    

    And your Button is now gonna look like:

    <Button    
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/my_string_resource"/>
    

    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:

    <string name="my_string_resource">Text to add as string resource</string>
    

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

    0 讨论(0)
  • 2021-02-07 10:43

    You have to switch to string.xml: its unfortunate, but right now Eclipse doesn't give you a clean way of popping into the string editor directly from the code you are typing. Optimally you would type a string constant (like R.string.new_string and I guess hotkey or double click or something and jump directly into the strings.xml editor with the existing entry selected (if new_string exists) or a new entry created (if new_string doesn't yet exist).

    Wouldn't that be nice.

    0 讨论(0)
  • 2021-02-07 10:44

    Eclipse will sort of do it for you. So if you have a field:

    android:text="hello"
    

    Select "hello" and then goto Refactor-->Android-->Extract Android String, Eclipse will change the line to:

    android:text="@string/hello"
    

    and automagically add the line to strings.xml as:

    <string name="hello">Hello</string>
    

    JAL

    0 讨论(0)
提交回复
热议问题