Android TextView : “Do not concatenate text displayed with setText”

前端 未结 10 1491
小鲜肉
小鲜肉 2020-12-02 04:01

I am setting text using setText() by following way.

prodNameView.setText(\"\" + name);

prodOriginalPriceView.setText(\"\" + String.format(g         


        
相关标签:
10条回答
  • 2020-12-02 04:54

    Do not concatenate text inside your setText() method, Concatenate what ever you want in a String and put that String value inside your setText() method.

    ex: correct way

    int min = 120;
    int sec = 200;
    int hrs = 2;
    
    String minutes = String.format("%02d", mins);
                String seconds = String.format("%02d", secs);
                String newTime = hrs+":"+minutes+":"+seconds;
    
    text.setText(minutes);
    

    Do not concatenate inside setText() like

    text.setText(hrs+":"+String.format("%02d", mins)+":"+String.format("%02d", secs));
    
    0 讨论(0)
  • 2020-12-02 04:57

    If you don't need to support i18n, you can disable this lint check in Android Studio

    File -> Settings -> Editor -> Inspections -> Android -> Lint -> TextView Internationalization(uncheck this)

    0 讨论(0)
  • 2020-12-02 04:58

    I ran into the same lint error message and solved it this way.

    Initially my code was:

    private void displayQuantity(int quantity) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + quantity);
    }
    

    I got the following error

    Do not concatenate text displayed with setText. Use resource string with placeholders.
    

    So, I added this to strings.xml

    <string name="blank">%d</string>
    

    Which is my initial "" + a placeholder for my number(quantity).

    Note: My quantity variable was previously defined and is what I wanted to append to the string. My code as a result was

    private void displayQuantity(int quantity) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText(getString(R.string.blank, quantity));
    }
    

    After this, my error went away. The behavior in the app did not change and my quantity continued to display as I wanted it to now without a lint error.

    0 讨论(0)
  • 2020-12-02 05:03

    Don't get confused with %1$s and %2$d in the accepted answer.Here is a few extra information.

    • The format specifiers can be of the following syntax:

    %[argument_index$]format_specifier

    1. The optional argument_index is specified as a number ending with a “$” after the “%” and selects the specified argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.
    2. The required format specifier is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.

    Example

    We will create the following formatted string where the gray parts are inserted programmatically.

    Hello Test! you have 0 new messages

    Your string resource:

    < string name="welcome_messages">Hello, %1$s! You have %2$d new messages< /string >

    Do the string substitution as given below:

    getString(R.string.welcome_message, "Test", 0);

    Note:

    • %1$s will be substituted by the string "Test"
    • %2$d will be substituted by the string "0"
    0 讨论(0)
提交回复
热议问题