使用Bmob服务器完成登陆注册!

效果如图所示

首先完成前期着呗工作,这里不做太多描述,详情见Bmob官方文档点击这里官方文档

登陆页面
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication2.MainActivity">

    <EditText
        android:id="@+id/zhanghao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/mima"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:text="登陆"

        android:id="@+id/denglu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:text="注册"
        android:id="@+id/zhuce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
写的比较简单
登陆的逻辑代码
public class Main2Activity extends AppCompatActivity {
    private EditText namea,mimaa;
    Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "Your Application ID
");
        setContentView(R.layout.register);
        namea=(EditText)findViewById(R.id.name);
        mimaa=(EditText)findViewById(R.id.mima);
       mButton=(Button)findViewById(R.id.zhuce1);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           String etzhanghao=namea.getText().toString();
           String etmima=mimaa.getText().toString();
            User o=new User();
             o.setUsername(etzhanghao);
             o.setPassword(etmima);
             o.signUp(new SaveListener<User>() {
                    @Override
                    public void done(User user, cn.bmob.v3.exception.BmobException e) {

                        if (e == null) {
                            Toast.makeText(Main2Activity.this, "注册成功!", Toast.LENGTH_LONG).show();


                        } else {
                            Toast.makeText(Main2Activity.this, "注册失败!", Toast.LENGTH_LONG).show();

                    }
                }
            });
        }
    });
}
     public   void fanhui(View v){
        startActivity(new Intent(this,MainActivity.class));
    }



}
注意上面的
Your Application ID换成你自己的的ID,每一个项目都有独立的ID;
注册页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/mima"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:text="注册"
        android:id="@+id/zhuce1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:onClick="fanhui"
        android:text="返回"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
注册的逻辑页面
public class Main2Activity extends AppCompatActivity {
    private EditText namea,mimaa;
    Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "
Your Application ID
");

setContentView(R.layout.register); namea=(EditText)findViewById(R.id.name); mimaa=(EditText)findViewById(R.id.mima); mButton=(Button)findViewById(R.id.zhuce1); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String etzhanghao=namea.getText().toString(); String etmima=mimaa.getText().toString(); User o=new User(); o.setUsername(etzhanghao); o.setPassword(etmima); o.signUp(new SaveListener<User>() { @Override public void done(User user, cn.bmob.v3.exception.BmobException e) { if (e == null) { Toast.makeText(Main2Activity.this, "注册成功!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(Main2Activity.this, "注册失败!", Toast.LENGTH_LONG).show(); } } }); } });} public void fanhui(View v){ startActivity(new Intent(this,MainActivity.class)); }}
注意上面的
Your Application ID换成你自己的的ID,每一个项目都有独立的ID;
注意我这里的User是集成的
BmobUser


这就完成了,不过貌似还有很多种方法能完成注册和登陆,大家可以去尝试。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值