史上最最简单的登录注册方法(Bomb后端云)



                                    史上最最简单的登录注册方法(Bomb后端云)

   学习安卓开发快一年了吧,写了很多很多次登录注册,感觉吧每次写都要花上很久,所以想着怎么样才能让它变得简单点,所以最近在浏览各大论坛时在上面看到了几位大神使用Bmod后端云来做这个登录注册,刚开始我一看这个名字感觉就跟高端,所以抱着试一试的心态去学了一下,果不其然,是真的简单,不多说了 上图

             首先在在Bmob上面注册一个账号:https://www.bmob.cn/login
             然后在里面创建应用,后续请看开发文档
             
            
          话不多说,上代码
         
          登录这块
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    TextView tv_register;
    EditText et_name,et_password;
    Button btn_login;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Bmob.initialize(this, "9d2b932612d7936e4cad1e81168b074d");
        tv_register=(TextView)findViewById(R.id.tv_register);
        btn_login=(Button)findViewById(R.id.btn_login);
        et_name=(EditText)findViewById(R.id.et_login_username);
        et_password=(EditText)findViewById(R.id.et_login_password);

        tv_register.setOnClickListener(this);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_register:
                Intent intent=new Intent(LoginActivity.this,RegistrationActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                String name = et_name.getText().toString();
                String password = et_password.getText().toString().trim();

                if(name.equals("")||password.equals("")){
                    Toast.makeText(LoginActivity.this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                BmobUser user=new BmobUser();
                user.setUsername(name);
                user.setPassword(password);

                user.login(new SaveListener() {
                    @Override
                    public void done(BmobUser bmobuser, BmobException e) {

                        if(e==null){
                            Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

                        }
                    }


                });
                break;




        }

    }
}


            用户类继承BmobUser
       这里可写可不写

/**
 * Created by Administrator on 2017/5/14.
 */

public class User extends BmobUser {




}



          注册这块
public class RegistrationActivity extends AppCompatActivity {

    EditText edit_name, edit_password, edit_the_second_password;
    Button btn_registration;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regisration);
        Bmob.initialize(this, "9d2b932612d7936e4cad1e81168b074d");

        edit_name = (EditText) findViewById(R.id.edit_name);
        edit_password = (EditText) findViewById(R.id.edit_password);
        edit_the_second_password = (EditText) findViewById(R.id.edit_the_second_password);
        btn_registration = (Button) findViewById(R.id.btn_registration);

        btn_registration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = edit_name.getText().toString();
                String password = edit_password.getText().toString();

                if(username.equals("")||password.equals("")){
                    Toast.makeText(RegistrationActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();
                    return;

                }

                BmobUser user=new BmobUser();
                user.setUsername(username);
                user.setPassword(password);

                user.signUp(new SaveListener(){
                    @Override
                    public void done(BmobUser s, BmobException e) {
                        if(e==null){
                            Intent intent=new Intent(RegistrationActivity.this,LoginActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(RegistrationActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                });


            }
        });
    }
}
 
  

你可能感兴趣的:(安卓开发)