android id标签,Android开发中的findViewById(R.id.textView)

本文探讨了在编程中过度使用反射的弊端,包括性能开销、安全限制和内部暴露等问题,并提供了替代方案。作者分享了一种中国特色的编码方式,以及个人实践的两种快速编码方法,强调提高代码可读性和效率的重要性。

6e66e5aa44cc5cda2acfc74d8ded2bec.png

如果已经敲了几年代码,这样写会不会显得太Low

以下提供几种改良办法:

一、首先提到反射:

反射不太好,性能开销大,官方文档说过的。不是迫不得已不用反射,直接略过。附上副作用的官方文档:“Drawbacks of Reflection

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

Performance OverheadBecause reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.Security RestrictionsReflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.Exposure of InternalsSince reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.“

二、中国特色法:(逗比法,仅限逗比参考,慎用)

介绍一种我在知乎上搜到的野路子,绝壁是跨界运用大神,直接上图:

334da4f153a394619de13e39f6583d3b.png

三、说说,我是怎么用的

方法一:(推荐使用)

5191860f96304577832eaf5223c27dc7.png

定义一个f(int id)方法:

private T f(int viewId) {

return (T) findViewById(viewId);

}

//可以直接写成

方法二:快捷键(可阅读性最好)

fbc+Tab

①局部变量时候.var

8e0e1101a59df659f4b8f86410cee05f.png

②全局变量时候.field

931f9a734b74a58fa009d3915a952fe8.png

按下Enter后选择第二个

32ba94fca35023cff4270322b337e529.png

此时命名控件

2d6a57bfd03f22f5ca0bab356fb13e81.png

另外Toast常用快捷键

426a666171d2585f0cf47d5d5aa2b24f.png

按下Enter键,输入吐司内容即可

b9684c4c9441c6860a9404796ec8ac3a.png

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值