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.getText());
*
*
*
*
*
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());
把每一小步,拆分出来。逻辑更加清晰,日后维护更加方便