因为是登录界面,所以仍然是需要处理登录的逻辑,同
SplashActivity代码类似(参见《4、闪屏后登陆》),这里会在用户点击登录按钮时处理登录的逻辑:
case R.id.btn_login:
getLogin();
break;
private void getLogin() {整体逻辑和SplashActivity差不多,这里出现了几个知识点:
String userName = et_usertel.getText().toString().trim();
String password = et_password.getText().toString().trim();
getLoadingDialog("正在登录...").show();
getLogin(userName, password);
}
private void getLogin(final String userName, final String password) {
if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password)) {
RequestParams params = new RequestParams();
params.put("username", userName);
params.put("password", DES.md5Pwd(password));
getLoadingDialog("正在登录... ").show();
netClient.post(Constants.Login_URL, params, new BaseJsonRes() {
@Override
public void onMySuccess(String data) {
Utils.putValue(LoginActivity.this, Constants.UserInfo, data);
Utils.putBooleanValue(LoginActivity.this, Constants.LoginState, true);
Utils.putValue(LoginActivity.this, Constants.NAME, userName);
Utils.putValue(LoginActivity.this, Constants.PWD, DES.md5Pwd(password));
getChatService(userName, DES.md5Pwd(password));
}
@Override
public void onMyFailure() {
getLoadingDialog("正在登录").dismiss();
}
});
} else {
Utils.showLongToast(LoginActivity.this, "请填写账号或密码!");
}
}
private void getChatService(final String userName, final String password) {
EMChatManager.getInstance().login(userName, password, new EMCallBack() {// 回调
@Override
public void onSuccess() {
runOnUiThread(new Runnable() {
public void run() {
Utils.putBooleanValue(LoginActivity.this, Constants.LoginState, true);
Utils.putValue(LoginActivity.this, Constants.User_ID, userName);
Utils.putValue(LoginActivity.this, Constants.PWD, password);
Log.d("main", "登陆聊天服务器成功!");
// 加载群组和会话
EMGroupManager.getInstance().loadAllGroups();
EMChatManager.getInstance().loadAllConversations();
getLoadingDialog("正在登录...").dismiss();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);
finish();
}
});
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登陆聊天服务器失败!");
runOnUiThread(new Runnable() {
public void run() {
getLoadingDialog("正在登录...").dismiss();
Utils.showLongToast(LoginActivity.this, "登陆失败!");
}
});
}
});
}
addTextChangedListener
BaseActivity
BasicNameValuePair
帮助页面:WebViewActivity
BaseJsonRes
NetClient
getLoadingDialog(FlippingLoadingDialog)
先说下addTextChangedListener,下面的几个后面逐个说。
用户名和密码输入框设置了一个事件,当用户输入符合长度的时候才启用登录按钮,否则灰化登录按钮,用以进行用户名和密码的弱校验:
et_usertel.addTextChangedListener(new TextChange());
et_password.addTextChangedListener(new TextChange());
// EditText监听器
class TextChange implements TextWatcher {
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
boolean Sign2 = et_usertel.getText().length() > 0;
boolean Sign3 = et_password.getText().length() > 4;
if (Sign2 & Sign3) {
btn_login.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_bg_green));
btn_login.setEnabled(true);
} else {
btn_login.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_enable_green));
btn_login.setTextColor(0xFFD0EFC6);
btn_login.setEnabled(false);
}
}
}