android代码整理

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());

把每一小步,拆分出来。逻辑更加清晰,日后维护更加方便

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值