Lost and Found(结对项目)登录注册

利用Bmob实现登录和注册首先要在bmob官网上下载对应的sdk,导入到libs里面就可以了。需要在bmob上建立应用,得到id(这些在前一博客中已经提到)所以话不多说直接上代码。

注册按钮

//注册点击
	public void dregi(View view){
		Intent intentlogin = new Intent();
		intentlogin.setClass(SecondActivity.this,MainActivity.class);
		startActivity(intentlogin);

登录按钮

//登录点击
    public void dlogin(View view){
        String name=mname.getText().toString();
        String password=mpassword.getText().toString();
        if(name.equals("")||password.equals("")){
            Toast.makeText(this, "帐号或密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        BmobQuery<user> query=new BmobQuery<user>();
        query.addWhereEqualTo("name", name);
        query.addWhereEqualTo("password", password);
        query.findObjects(new FindListener<user>() {
            
            @Override
            public void done(List<user> arg0, BmobException e) {
                // TODO Auto-generated method stub
                if(e==null){
                String gname=arg0.get(0).getName().toString();
                String gpassword=arg0.get(0).getPassword().toString();
                String name=mname.getText().toString();
                String password=mpassword.getText().toString();
                Toast.makeText(SecondActivity.this, gname, Toast.LENGTH_LONG).show();
                if(gname.equals(name)&&gpassword.equals(password))
                {
                    Intent seccess = new Intent();
                    seccess.setClass(SecondActivity.this, ThridActivity.class);
                    startActivity(seccess);
                }
                
                }
                else{
                    Toast.makeText(SecondActivity.this, "帐号或密码有误", Toast.LENGTH_LONG).show();
                }
                

返回登录页面

public void ret(View view){
     
        Intent intent1 = new Intent();
        intent1.setClass(MainActivity.this, SecondActivity.class);
        MainActivity.this.startActivity(intent1);
        
        
    }

点击注册

//点击注册
    public void zhuce(View view){
    
        String name=mname.getText().toString();
        String password=mpassword.getText().toString();
        String number=mnumber.getText().toString();
        String verify=mverify.getText().toString();
        if(name.equals("")||password.equals(""))
        {
            Toast.makeText(this, "帐号或密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if(name.length()<6)
        {
            Toast.makeText(this, "帐号小于6位", Toast.LENGTH_LONG).show();
            return;
        }
        if(verify.length()==0)
        {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if(number.length()==0)
        {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if(number.length()!=11)
        {
            Toast.makeText(this, "请输入11位有效号码", Toast.LENGTH_LONG).show();
            return;
        }
        BmobSMS.verifySmsCode(this, number, verify, new VerifySMSCodeListener() {
            
            @Override
            public void done(cn.bmob.sms.exception.BmobException e) {
                // TODO Auto-generated method stub
                if (e == null) {  
                    Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); 
                  //验证码正确 添加用户信息
                    String name=mname.getText().toString();
                    String password=mpassword.getText().toString();
                    user User=new user();
                    User.setName(name);
                    User.setPassword(password);
                    User.save(new SaveListener<String>() {

                        @Override
                        public void done(String arg0, BmobException arg1) {
                            // TODO Auto-generated method stub
                            if(arg1==null){
                                return;
                                
                            }else{
                                return;
                            }
                        }
                        
                    });
                    
                    Intent intent2 = new Intent();
                    intent2.setClass(MainActivity.this, SecondActivity.class);
                    MainActivity.this.startActivity(intent2);
                }  
                else {   
                    Toast.makeText(MainActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();  
                }  
            }
        });

点击获取验证码

public void gsms(View view){
        String number=mnumber.getText().toString();
        if(number.length()==0)
        {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if(number.length()!=11)
        {
            Toast.makeText(this, "请输入11位有效号码", Toast.LENGTH_LONG).show();
            return;
        }
        BmobSMS.requestSMSCode(this, number, "短信模板", new RequestSMSCodeListener() {
            
            @Override
            public void done(Integer integer, cn.bmob.sms.exception.BmobException e) {
                // TODO Auto-generated method stub
                 if (e == null) {  
                     //发送成功时,让获取验证码按钮不可点击,且为灰色  
                     mgetsms.setClickable(false);  
                     //mgetsms.setBackgroundColor();  
                     Toast.makeText(MainActivity.this, "验证码发送成功,请尽快使用", Toast.LENGTH_SHORT).show();  
                     /** 
                      * 倒计时1分钟操作 
                      * 说明: 
                      * new CountDownTimer(60000, 1000),第一个参数为倒计时总时间,第二个参数为倒计时的间隔时间 
                      * 单位都为ms,其中必须要实现onTick()和onFinish()两个方法,onTick()方法为当倒计时在进行中时, 
                      * 所做的操作,它的参数millisUntilFinished为距离倒计时结束时的时间,最后要注意的是当new CountDownTimer(60000, 1000)之后,一定要调用start() 
                      * 方法把该倒计时操作启动起来,不调用start()方法的话,是不会进行倒计时操作的 
                      */  
                     new CountDownTimer(60000, 1000) {  
                         @Override  
                         public void onTick(long millisUntilFinished) {  
                             //Message_btn.setBackgroundResource(R.drawable.button_shape02);  
                             mgetsms.setText(millisUntilFinished / 1000 + "");  
                         }  

                         @Override  
                         public void onFinish() {  
                             mgetsms.setClickable(true);  
                             //Message_btn.setBackgroundResource(R.drawable.button_shape);  
                             mgetsms.setText("重新发送");  
                         }  
                     }.start();   
                 }  
                 else {  
                     Toast.makeText(MainActivity.this, "验证码发送失败,请检查网络连接", Toast.LENGTH_SHORT).show();  
                 }  
                

 

posted @ 2017-06-05 00:25  柚栯  阅读(351)  评论(0编辑  收藏  举报