Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

接上一篇,加入验证用户名是否已经注冊功能!

仅仅须要改动MainActivity.java:

package com.example.logintest;

import java.util.List;

import org.w3c.dom.UserDataHandler;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText mUserName,mPassword; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bmob.initialize(this, "8f3ffb2658d8a3366a70a0b0ca0b71b2");
mUserName=(EditText) findViewById(R.id.et_username);
mPassword=(EditText) findViewById(R.id.et_password);
}
public void submit(View view){
final String username=mUserName.getText().toString();
final String password=mPassword.getText().toString();
if(username.equals("")||password.equals("")){
Toast.makeText(this, "用户名或密码不能为空!", 3).show();
}else{
BmobQuery<User> query =new BmobQuery<User>();//查询类
query.addWhereEqualTo("userName", username);//查询条件
query.findObjects(MainActivity.this, new FindListener<User>() { @Override
public void onSuccess(List<User> userlist) {
if(userlist.size()==0){//查询不到,用户名可用
User user=new User();
user.setUserName(username);
user.setUserPassword(password);
user.save(MainActivity.this,new SaveListener() { @Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "注冊成功! ", 3).show(); } @Override
public void onFailure(int arg0, String arg1) {
Toast.makeText(MainActivity.this, "注冊失败!", 3).show();
}
});
}else {
Toast.makeText(MainActivity.this, "用户名已被注冊。", 3).show();
} } @Override
public void onError(int arg0, String arg1) {
// TODO Auto-generated method stub }
}); } } }

执行实比例如以下:

Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

喜欢的朋友关注我,谢谢!

上一篇:python随机数random


下一篇:Jerry和您聊聊Chrome开发者工具