使用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
这就完成了,不过貌似还有很多种方法能完成注册和登陆,大家可以去尝试。
 
  
 
  

猜你喜欢

转载自blog.csdn.net/qq_33282116/article/details/53884977
今日推荐