基于Baas实现用户的注册功能。
我们使用Bmob提供的API进行实战开发,首先在Bmob官网上下载SDK,然后将jar包拷入工程内。
创建应用,获取应用key:
1.main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TableLayout android:id="@+id/tl" android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/et_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="50dp" android:text="" /> </TableRow> <TableRow> <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:id="@+id/et_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="50dp" android:text="" /> </TableRow> </TableLayout> <Button android:id="@+id/register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tl" android:onClick="submit" android:text="注册" /> </RelativeLayout>
2.MainActivity.java:
package com.example.logintest; import org.w3c.dom.UserDataHandler; import cn.bmob.v3.Bmob; 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");//初始化,第二项为官网创建应用的key. mUserName=(EditText) findViewById(R.id.et_username); mPassword=(EditText) findViewById(R.id.et_password); } public void submit(View view){//点击事件 String username=mUserName.getText().toString(); String password=mPassword.getText().toString(); if(username.equals("")||password.equals("")){ Toast.makeText(this, "用户名或密码不能为空!", 3).show(); }else{ 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(); } }); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.logintest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_LOGS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.logintest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>4.javabean.java:
package com.example.logintest; import cn.bmob.v3.BmobObject; public class User extends BmobObject { private String userPassword; public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } private String userName; }
我们看一下官网的数据浏览,可以发现多了一个User表格,里面有两条注册数据,注册成功啦!
喜欢的朋友可以关注我!多谢