Android practice simple tutorial-22nd gun (Baas-based user registration function), androidbaas

Source: Internet
Author: User

Android practice simple tutorial-22nd gun (Baas-based user registration function), androidbaas

Baas-based user registration.

We use the APIS provided by Bmob for actual development. First, download the SDK on the Bmob official website, and then upload the jar package to the project.

Create an application and obtain the application 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 = "username:"/> <EditText android: id = "@ + id/et_username" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: minWidth = "50dp" android: text = ""/> </TableRow> <TextView android: id = "@ + id/TV _password" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Password:"/> <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 = "register"/> </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; @ Overrideprotected void onCrea Te (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "8f3ffb2658d8a3316a70a0b0ca0b71b2"); // initialize the application. The second item is the key used to create an application on the official website. mUserName = (EditText) findViewById (R. id. et_username); mPassword = (EditText) findViewById (R. id. et_password);} public void submit (View view) {// Click Event String username = mUserName. getText (). toString (); String password = mPassword. ge TText (). toString (); if (username. equals ("") | password. equals ("") {Toast. makeText (this, "the user name or password cannot be blank! ", 3 ). show ();} else {User user = new User (); user. setUserName (username); user. setUserPassword (password); user. save (MainActivity. this, new SaveListener () {@ Overridepublic void onSuccess () {Toast. makeText (MainActivity. this, "registration successful! ", 3). show () ;}@ Overridepublic void onFailure (int arg0, String arg1) {Toast. makeText (MainActivity. this," registration failed! ", 3). show ();}});}}}

3. Configure permissions:

<?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;}

Running instance:

Let's take a look at the data browsing on the official website. We can find an additional User table, which contains two registration data items. The registration is successful!

If you like it, follow me! Thanks

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.