接上一篇,加入验证用户名是否已经注冊功能!
仅仅须要改动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{ BmobQueryquery =new BmobQuery ();//查询类 query.addWhereEqualTo("userName", username);//查询条件 query.findObjects(MainActivity.this, new FindListener () { @Override public void onSuccess(List 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 } }); } } }
执行实比例如以下:喜欢的朋友关注我,谢谢!