
在Android開發中,比如我們需要有一個驗證碼倒計時,
很多開發的時候都會這樣寫
int count = 60;
textView.setText(""+count);
count--;
handler.sendxxxx;
這樣每組合一個數字都會生成一個新的字串 會重新在堆疊開辟空間
優化方式:
String.format()
tvReSendMessage.setText(String.format(context.getResources().getString(R.string.re_send_message_format),count));
re_send_message_format:
重新發送 %1$d
%1 對應是第幾個引數,$d代表 第一個需要格式化的引數是整形
%d 整形
%s 字符型
%f 浮點型
如果是多引數
可以寫成:
你的名字 %1$s %2$d %3$f
如果有C/C++基礎的童鞋一定會知道
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/347161.html
標籤:其他
