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

前端 未结 10 1516
小鲜肉
小鲜肉 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: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

    %d
    

    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.

提交回复
热议问题