login 登陆

login写法范例: 
rivate void login() {
  // // 单机版
  // Intent intent = new Intent();
  // intent.setClass(AccountLoginActivity.this, MainTabActivity.class);
  // startActivity(intent);
  // AccountLoginActivity.this.finish();

  // 联网版
  if (isLegal()) {
   showWaiting(isWaiting);
   String userName = mUsernameEditText.getText().toString();
   String password = mPasswordEditText.getText().toString();

   RequestParams params = new RequestParams();
   params.add("policeNo", userName);
   params.add("policePassword", password);
   HttpUtils.postAsync(ServerAPI.URL_LOGIN, params, loginResponseHandler);
  }

 }

 private boolean isLegal() {
  boolean legal = true;
  String msg = null;
  String username = mUsernameEditText.getText().toString();
  String password = mPasswordEditText.getText().toString();
  if (username == null || username.length() == 0) {
   legal = false;
   msg = getResources().getString(R.string.user_error_msg1);
  }
  else if (password == null || password.length() == 0) {
   legal = false;
   msg = getResources().getString(R.string.user_error_msg2);
  }
  else if (password.length() < 6 || password.length() > 20) {
   legal = false;
   msg = getResources().getString(R.string.user_error_msg3);
  }
  showErrorMsg(msg);
  return legal;
 }

 private void showErrorMsg(String msg) {
  hideWaiting();
  if (msg == null) {
   if (mMessageBox.isShown())
    mMessageBox.setVisibility(View.INVISIBLE);
  }
  else {
   if (!mMessageBox.isShown()) {
    mMessageBox.setVisibility(View.VISIBLE);
   }
   mMessageBox.setText(msg);
  }
 }

 TextHttpResponseHandler loginResponseHandler = new TextHttpResponseHandler() {

  @Override
  public void onFailure(int statusCode, Header[] headers, String responseString,
          Throwable throwable) {

   Log.w(TAG, statusCode + " Login:" + responseString);
   // hideWaiting();
   showErrorMsg("网络连接失败,请重试!" + statusCode);
  }

  @Override
  public void onSuccess(int statusCode, Header[] headers, String responseString) {
   // hideWaiting();
   Log.i(TAG, "Login:" + responseString);

   LoginHttpBean loginHttpBean = new Gson().fromJson(responseString, LoginHttpBean.class);
   if ("200".equals(loginHttpBean.getCode())) {
    BaseApplication.appVersionList.addAll(loginHttpBean.getAppApplicationAllId());
    policeInfoList.addAll(loginHttpBean.getPoliceInfoBean());

    BaseApplication.policeInfoList = policeInfoList;
    // savePoliceBean(loginHttpBean.getPoliceInfoBean().get(0));
    BaseApplication.savePoliceBean(responseString);

    Intent intent = new Intent();
    intent.setClass(AccountLoginActivity.this, MainTabActivity.class);
    startActivity(intent);
    hideWaiting();// 隐藏登录过程中显示的loading
    AccountLoginActivity.this.finish();
   }
   else {
    showErrorMsg(loginHttpBean.getCodeMsg());
   }
  }
 };

 // public void savePoliceBean(PoliceBean policeBean) {
 // SharedPreferences mSharedPreferences = getSharedPreferences("police",
 // Context.MODE_PRIVATE);
 // SharedPreferences.Editor editor = mSharedPreferences.edit();
 // editor.putInt("policeId", policeBean.getId());
 // editor.commit();
 // }

//显示login那个画面 在activity上面新建层来出现login画面
 protected final void showWaiting(boolean isBlur) {
  isWaiting = true;
  try {
   WindowManager.LayoutParams layoutParams = null;
   if (isBlur) {
    layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_BLUR_BEHIND
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT);
   }
   else {
    layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT);
   }

   if (loadView == null) {
    LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//载入login显示的布局
    loadView = inflate.inflate(R.layout.view_loading, null);
   }

   windowManager.addView(loadView, layoutParams);
  }
  catch (Throwable e) {
   e.printStackTrace();
   isWaiting = false;
  }
 }

 protected final void hideWaiting() {
  isWaiting = false;
  try {
   if (loadView != null) {

//移除login层
    windowManager.removeView(loadView);
    loadView = null;
   }
  }
  catch (Throwable e) {
   e.printStackTrace();
  }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值