因为登陆界面有注册功能要能转到账号的注册界面,所以先完成了注册界面的布局,内容包括输入账号、输入密码、确定密码以及用已有账号登陆等等。
注册界面的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@drawable/splash"
android:orientation="vertical">
<include layout="@layout/title_layout"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@mipmap/icon_user_account" />
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@null"
android:hint="请输入账号"
android:digits="@string/digits"
android:inputType="textEmailAddress"
android:textColor="@android:color/white"
android:textColorHint="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="7dp"
android:background="@color/hint_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_marginLeft="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@mipmap/icon_user_name" />
<EditText
android:id="@+id/et_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@null"
android:hint="请输入昵称"
android:inputType="text"
android:textColor="@android:color/white"
android:textColorHint="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="7dp"
android:background="@color/hint_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_marginLeft="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@mipmap/icon_pass_word" />
<EditText
android:id="@+id/et_pass_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@null"
android:hint="请输入密码"
android:digits="@string/digits"
android:inputType="textPassword"
android:textColor="@android:color/white"
android:textColorHint="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="7dp"
android:background="@color/hint_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_marginLeft="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@mipmap/icon_confirm_pass" />
<EditText
android:id="@+id/et_confirm_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@null"
android:hint="请确认密码"
android:digits="@string/digits"
android:inputType="textPassword"
android:textColor="@android:color/white"
android:textColorHint="@color/white_color"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="7dp"
android:background="@color/hint_color" />
<TextView
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="46dp"
android:background="@drawable/login_nol_bg"
android:gravity="center"
android:text="@string/register"
android:textColor="#1A8ECB"
android:textSize="16sp"/>
<TextView
android:id="@+id/btn_to_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="@string/have_account_login"
android:textColor="@color/white_color"
android:textSize="14sp" />
<View
android:layout_width="120dp"
android:layout_height="0.8dp"
android:layout_gravity="center_horizontal"
android:background="@color/white_color" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="6dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:textColor="@color/app_blue_color"
android:text="@string/app_version"/>
</RelativeLayout>
</LinearLayout>
为注册按键绑定监听器,内容包括检测信息是否填充完整,如果有空缺则提示,信息合法后创建账号
@OnClick(R.id.btn_register)
public void register(){
String account = mEtAccount.getText().toString().trim();
String name = mEtName.getText().toString().trim();
String pass = mEtPass.getText().toString().trim();
String confirmPass = mEtConfirmPass.getText().toString().trim();
if (TextUtils.isEmpty(account) || TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)){
Toast.makeText(this,"请将信息填写完整",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(confirmPass) || !confirmPass.equals(pass)){
ToastUtils.showMessage(this,"确认密码为空或与密码不符");
return;
}
NimClientHandle.getInstance().register(account,pass, name, new OnRegisterListener() {
@Override
public void onSuccess() {
ToastUtils.showMessage(RegisterActivity.this,"注册成功");
finish();
}
@Override
public void onFailed(String message) {
ToastUtils.showMessage(RegisterActivity.this,"注册失败:" + message);
}
});
}
然后在上次自定义的LoginActivity中实现按注册键后,跳转到注册界面的功能
@OnClick(R.id.tv_btn_register)
public void startRegister(){
startActivity(new Intent(this,
RegisterActivity.class));
}
利用上次自定义的SharesPerferecesUtil类来实现保存登陆信息的方法,然后会在下面登陆功能中国调用
private void saveLoginInfo(LoginInfo info){
SharedPreferencesUtil.setStringSharedPreferences(this, Constant.LOCAL_LOGIN_TABLE,
Constant.LOCAL_USER_ACCOUNT,info.getAccount());
SharedPreferencesUtil.setStringSharedPreferences(this, Constant.LOCAL_LOGIN_TABLE,
Constant.LOCAL_USER_TOKEN,info.getToken());
NimUserHandler.getInstance().setMyAccount(info.getAccount());//**内部仔细看
}
为登陆按键实现跳转到个人账号的功能,如果账号或密码不合法,则弹出提示且登陆无效
@OnClick(R.id.tv_btn_login)
public void login(){
String account = mEtUserAccount.getText().toString().trim();
String pass = mEtPassWord.getText().toString().trim();
if (TextUtils.isEmpty(account) || TextUtils.isEmpty(pass)){
ToastUtils.showMessage(this,"账号或密码为空~");
return;
}
RequestCallback<LoginInfo> callBack = new RequestCallback<LoginInfo>() {
@Override
public void onSuccess(LoginInfo loginInfo) {
isLogin = false;
// 保存登录信息
saveLoginInfo(loginInfo);
// 转入主页面
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}
@Override
public void onFailed(int code) {
isLogin = false;
ToastUtils.showMessage(LoginActivity.this,
"登录失败:"+ ConvertUtils.code2String(code));
}
@Override
public void onException(Throwable exception) {
isLogin = false;
ToastUtils.showMessage(LoginActivity.this,
"登录出错:"+exception.getMessage());
}
};
LoginInfo loginInfo = new LoginInfo(account,pass);
mLoginFuture = NIMClient.getService(AuthService.class).login(loginInfo);
isLogin = true;
mLoginFuture.setCallback(callBack);
}
实现退出程序的功能,计算点击次数,点击两次才可成功退出,避免用户错按
private void backDoubleExit(){
mKeyBackCount++;
if (mKeyBackCount == 1){
ToastUtils.showMessage(LoginActivity.this,"再点一次退出程序~~");
}else if (mKeyBackCount == 2){
MyApplication.getInstance().AppExit();
}
}