How to put variable inside string-resources?

后端 未结 4 395
情歌与酒
情歌与酒 2020-12-08 18:07

Is it possible to put variables inside string-resources? And if yes - how do i use them.

What i need is the following:

相关标签:
4条回答
  • 2020-12-08 18:35
    <string name="meatShootingMessage">You shot %1$d pounds of meat! Put Second Variable String here %2$s and third variable integer here %3$d</string>
    int intVariable1 = 123;
    String stringVariable2 = "your String";
    int intVariable3 = 456;
    String strMeatFormat = getResources().getString(R.string.meatShootingMessage, intVariable1, stringVariable2 , intVaribale3);
    
    0 讨论(0)
  • 2020-12-08 18:37

    yes,you can use. after adding tag to string.xml file and then to retrrive the the same in your .java file you can follow this.

    AndriodApp

    String str = getResources().getString(R.string.name);

    0 讨论(0)
  • 2020-12-08 18:45
    <string name="meatShootingMessage">You shot %1$d pounds of meat!</string>  
    
    
    int numPoundsMeat = 123;
    String strMeatFormat = getResources().getString(R.string.meatShootingMessage, numPoundsMeat);
    

    Example taken from here

    0 讨论(0)
  • 2020-12-08 19:00

    Just pass it throught getString() function as formatArgs Object.

    int nextResultsSize = getNextResultsSize();
    String strNextResultsSize = 
         getResources().getString(R.string.next_x_results, nextResultsSize);
    

    XML:

    <string name="next_x_results">Next %1$d results</string>
    
    0 讨论(0)
提交回复
热议问题