注册界面比较简单,一个账号,两个密码,一个按钮。
<!-- 注册 -->
<activity
android:name=".activity.RegisterActivity"
android:screenOrientation="portrait"
android:theme="@style/horizontal_slide" >
</activity>
/**
* 注册
* @param view
*/
public void register(View view) {
String st1 = getResources().getString(R.string.User_name_cannot_be_empty);
String st2 = getResources().getString(R.string.Password_cannot_be_empty);
String st3 = getResources().getString(R.string.Confirm_password_cannot_be_empty);
String st4 = getResources().getString(R.string.Two_input_password);
String st5 = getResources().getString(R.string.Is_the_registered);
final String st6 = getResources().getString(R.string.Registered_successfully);
final String username = userNameEditText.getText().toString().trim();
final String pwd = passwordEditText.getText().toString().trim();
String confirm_pwd = confirmPwdEditText.getText().toString().trim();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, st1, Toast.LENGTH_SHORT).show();
userNameEditText.requestFocus();
return;
} else if (TextUtils.isEmpty(pwd)) {
Toast.makeText(this, st2, Toast.LENGTH_SHORT).show();
passwordEditText.requestFocus();
return;
} else if (TextUtils.isEmpty(confirm_pwd)) {
Toast.makeText(this, st3, Toast.LENGTH_SHORT).show();
confirmPwdEditText.requestFocus();
return;
} else if (!pwd.equals(confirm_pwd)) {
Toast.makeText(this, st4, Toast.LENGTH_SHORT).show();
return;
}
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(pwd)) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage(st5);
pd.show();
final String st7 = getResources().getString(R.string.network_anomalies);
final String st8 = getResources().getString(R.string.User_already_exists);
final String st9 = getResources().getString(R.string.registration_failed_without_permission);
final String st10 = getResources().getString(R.string.Registration_failed);
new Thread(new Runnable() {
public void run() {
try {
// 调用sdk注册方法
EMChatManager.getInstance().createAccountOnServer(username, pwd);
runOnUiThread(new Runnable() {
public void run() {
if (!RegisterActivity.this.isFinishing())
pd.dismiss();
// 保存用户名
DemoApplication.getInstance().setUserName(username);
Toast.makeText(getApplicationContext(), st6, 0).show();
finish();
}
});
} catch (final EaseMobException e) {
runOnUiThread(new Runnable() {
public void run() {
if (!RegisterActivity.this.isFinishing())
pd.dismiss();
int errorCode=e.getErrorCode();
if(errorCode==EMError.NONETWORK_ERROR){
Toast.makeText(getApplicationContext(), st7, Toast.LENGTH_SHORT).show();
}else if(errorCode==EMError.USER_ALREADY_EXISTS){
Toast.makeText(getApplicationContext(), st8, Toast.LENGTH_SHORT).show();
}else if(errorCode==EMError.UNAUTHORIZED){
Toast.makeText(getApplicationContext(), st9, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), st10 + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
}).start();
}
}
开始都一样,对输入进行验证。
接下来进行注册,先显示一个进度条,然后开启线程,进行注册,在注册的时候如果界面没有关闭则保存用户名后关闭,其中runOnUiThread内部实现跟Handler一样。此处不详讲。对于Handler请看《Android中Looper,Message和Handle之间的简要关系说明》。