android代码整理
2016-06-23 00:05
459 查看
android代码整理
在需要跳转的activity内,定义intent/*在需要跳转的activity内,对intent属性进行定义。这样在第一个activity内,仅需要定义需求的参数*/ public static Intent getStartIntent(Context context, @NonNull String paramOne, @Nullable String paramTwo) { Intent intent = new Intent(context, XXXXActivity.class); intent.putExtra(EXTRA_PARAM_ONE, paramOne); if (paramTwo != null) { intent.putExtra(EXTRA_PARAM_TWO, paramTwo); } return intent; }
用/**/代替//来进行注释
推荐注释:
/*提交成功布局*/ private LinearLayout commitSuccessLayout;
不推荐注释:
private LinearLayout commitSuccessLayout;//提交成功布局
为每一个需要监听的view,量身定做listener,使逻辑更清晰
推荐方法:
private View.OnClickListener commitButtonClickListener = new View.OnClickListener() { @Override public void onClick(View v) { //TODO } }; private View.OnClickListener deleteButtonClickListener = new View.OnClickListener() { @Override public void onClick(View v) { //TODO } };
commitButton.setOnClickListener(commitButtonClickListener); deleteButton.setOnClickListener(deleteButtonClickListener);
不推荐方法:
commitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //TODO } }); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //TODO } }); } };
在使用局部变量的之前,再定义它
推荐方法:
String uid = String.valueOf(mLoginAccountEdit.getText()); if (TextUtils.isEmpty(uid)) { showToast("手机号码不能为空!", Toast.LENGTH_SHORT); return false; } * * * * * String pwd = String.valueOf(mLoginPwdEdit.getText()); if (TextUtils.isEmpty(pwd)) { showToast("密码不能为空!", Toast.LENGTH_SHORT); return false; } * * * * *
不推荐方法:
String uid = String.valueOf(mLoginAccountEdit.getText()); String pwd = String.valueOf(mLoginPwdEdit.getTex 4000 t()); * * * * * if (TextUtils.isEmpty(pwd)) { showToast("密码不能为空!", Toast.LENGTH_SHORT); return false; } if (TextUtils.isEmpty(uid)) { showToast("手机号码不能为空!", Toast.LENGTH_SHORT); return false; }
将复杂的步骤,拆分成多个小步骤
推荐的方法
public interface IFirstStep { public String openTheDoor(); }
public interface ISecondStep { String sendTheElephantIntoTheFridge(); }
public interface IThirdStep { String closeTheDoor(); }
public class FirstStep implements IFirstStep { @Override public String openTheDoor() { return "open the door"; } }
public class SecondStep implements ISecondStep{ @Override public String sendTheElephantIntoTheFridge() { return "send the elephant into the fridge"; } }
public class ThirdStep implements IThirdStep { @Override public String closeTheDoor() { return "close the door"; } }
public class CloseTheElephant { private IFirstStep iFirstStep; private ISecondStep iSecondStep; private IThirdStep iThirdStep; public CloseTheElephant(IFirstStep iFirstStep, ISecondStep iSecondStep, IThirdStep iThirdStep) { this.iFirstStep = iFirstStep; this.iSecondStep = iSecondStep; this.iThirdStep = iThirdStep; } public String Work() { String stepOne = iFirstStep.openTheDoor(); String stepTwo = iSecondStep.sendTheElephantIntoTheFridge(); String stepThree = iThirdStep.closeTheDoor(); return stepOne + stepTwo + stepThree; }
CloseTheElephant closeTheElephant = new CloseTheElephant(new FirstStep(), new SecondStep(), new ThirdStep()); Log.i("Dingo", closeTheElephant.Work());
把每一小步,拆分出来。逻辑更加清晰,日后维护更加方便
相关文章推荐
- 原来fastboot boot custom.img可以无需刷机就以启动定制系统(以root)
- Android线程,线程池使用及原理博文参考
- Android进程间通信机制Binder应用层分析
- Android 权限大全
- Android学习笔记函数
- Android文本读写
- 15 个 Android 通用流行框架大全
- Android之service和thread的区别
- android中的Packagemanager
- Android真机调试
- Android Studio导出Jar包的方法探究
- Android 设置theme中的坑
- android脸萌3
- Android 选择图片,加载图片,显示图片 综合案例
- Android系统透明栏(Translucent Bars)使用SystemBarTint的实现攻略
- android常见异常总结
- android开发脚本之过滤特定包名的log
- Android Development Notes-4(BroadcastReceiver, Manifests)
- Android init.rc文件浅析
- Android自定义组件之简单组合