Android practice simple tutorial-23rd gun (Baas-based user registration and login module implementation !), Androidbaas
Next, let's take a look at the code in the last two articles based on the APIS provided by Bmob.
1. registration page 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: layout_weight = "1" android: text = "username: "/> <EditText android: id =" @ + id/et_username "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_weight =" 5 "android: text = ""/> </TableRow> <TextView android: id = "@ + id/TV _password" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "Password:"/> <EditText android: id = "@ + id/et_password" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "5" 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. registration page. java:
Package com. example. logintest; import java. util. list; import org. w3c. dom. userDataHandler; import cn. bmob. v3.Bmob; import cn. bmob. v3.BmobQuery; import cn. bmob. v3.listener. findListener; import cn. bmob. v3.listener. saveListener; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. widget. editText; import android. widg Et. toast; public class MainActivity extends Activity {private EditText mUserName, mPassword; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "8f3ffb2658d8a3316a70a0b0ca0b71b2"); mUserName = (EditText) findViewById (R. id. et_username); mPassword = (EditText) findViewById (R. id. et_password);} public Void submit (View view) {final String username = mUserName. getText (). toString (); final String password = mPassword. getText (). toString (); if (username. equals ("") | password. equals ("") {Toast. makeText (this, "the user name or password cannot be blank! ", 3 ). show ();} else {BmobQuery <User> query = new BmobQuery <User> (); // query class query. addWhereEqualTo ("userName", username); // query condition query. findObjects (MainActivity. this, new FindListener <User> () {@ Overridepublic void onSuccess (List <User> userlist) {if (userlist. size () = 0) {// No query found. The User name can be user User = new user (); User. setUserName (username); user. setUserPassword (password); user. save (MainActivity. this, new SaveLis Tener () {@ Overridepublic void onSuccess () {Toast. makeText (MainActivity. this, "registration successful! Go to the logon page! ", 3 ). show (); Intent intent = new Intent (); intent. setClass (MainActivity. this, Login. class); intent. putExtra ("username", username); startActivity (intent) ;}@ Overridepublic void onFailure (int arg0, String arg1) {Toast. makeText (MainActivity. this, "registration failed! ", 3). show () ;}}) ;}else {Toast. makeText (MainActivity. this," the user name has been registered! ", 3). show () ;}@ Overridepublic void onError (int arg0, String arg1) {// TODO Auto-generated method stub }});}}}3. logon interface. 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/tllayout" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TableRow> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "username:"/> <EditText android: id = "@ + id/et_usernamelogin" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "5" android: text = ""/> </TableRow> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "Password:"/> <EditText android: id = "@ + id/et_passwordlogin" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: password = "true" android: layout_weight = "5" android: text = ""/> </TableRow> </TableLayout> <Button android: id = "@ + id/login" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/tllayout" android: text = "login"/> </RelativeLayout>
4. logon interface. java:
Package com. example. logintest; import java. util. list; import cn. bmob. v3.Bmob; import cn. bmob. v3.BmobQuery; import cn. bmob. v3.listener. findListener; import cn. bmob. v3.listener. saveListener; import android. app. activity; import android. content. dialogInterface; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; I Mport android. widget. editText; import android. widget. toast; public class Login extends Activity {private EditText mLoginName, mLoginPassword; private Button mButtonLogin; private String username, password; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. login); Bmob. initialize (this, "8f3ffb2658d8a3316a70a0b0ca0b71b2"); // initialize BmobmLog InName = (EditText) findViewById (R. id. et_usernamelogin); mLoginPassword = (EditText) findViewById (R. id. et_passwordlogin); mButtonLogin = (Button) findViewById (R. id. login); Intent intent = getIntent (); username = intent. getStringExtra ("username"); mLoginName. setText (username); mButtonLogin. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {final String username = mLoginName. g EtText (). toString (); final String password = mLoginPassword. getText (). toString (); if (username. equals ("") | password. equals ("") {Toast. makeText (Login. this, "the user name or password cannot be blank! ", 3 ). show ();} else {BmobQuery <User> query = new BmobQuery <User> (); // query class query. addWhereEqualTo ("userName", username); // query condition query. findObjects (Login. this, new FindListener <User> () {@ Overridepublic void onSuccess (List <User> userlist) {if (userlist = null) {// the User name cannot be queried. Toast is available. makeText (Login. this, "the user name is incorrect! ", 3 ). show ();} else {if (userlist. get (0 ). getUserPassword (). equals (password) {Toast. makeText (Login. this, "Logon successful! ", 3 ). show (); Intent intent = new Intent (); intent. setClass (Login. this, Success. class); startActivity (intent);} else {Toast. makeText (Login. this, "Incorrect password! ", 3 ). show () ;}}@ Overridepublic void onError (int arg0, String arg1) {// TODO Auto-generated method stub }});}}});}}
5. log on successfully. xml:
<? 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"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "Login successful! "/> </LinearLayout>
6. logon successful. java:
package com.example.logintest;import android.app.Activity;import android.os.Bundle;public class Success extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.success);}}
7. 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;}8. configuration file:
<?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> <activity android:name="com.example.logintest.Login" > </activity> <activity android:name="com.example.logintest.Success" > </activity> </application></manifest>
9. file:
10. Run:
If you like it, follow me! Thank you.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.