Android 使用Stringformat优化你的代码吧

Android 使用Stringformat优化你的代码吧,第1张

Android 使用Stringformat优化你的代码吧

在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 浮点型

%s 字符型

如果是多参数

可以写成:

你的名字  %1$s %2$d %3$f

如果有C/C++基础的童鞋一定会知道

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5116033.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存