Android Combat Simple Tutorial-the 23rd gun (based on Baas user registration and Login module implementation!) )

Source: Internet
Author: User
Tags gettext

Then on the last two articles, we implement the user login function based on the API provided by Bmob, and look at the code as a whole.

1. Register the 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 "> <Tabl  Elayout android:id= "@+id/tl" android:layout_width= "match_parent" android:layout_height= "Wrap_content" > <TableRow> <textview android:id= "@+id/tv_name" Android:layo                Ut_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "User name:"/> <edittext android:id= "@+id/et_username" an Droid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight = "5" android:text= ""/> </TableRow> <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&    Gt <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"/></relativ Elayout>

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.widget.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, "8f3ffb2658d8a3366a70a0b0ca0b71b2"); 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.ge Ttext (). toString (); if (Username.equals ("") | | Password.equals ("")) {Toast.maketext (tHis, "The user name or password cannot be empty!" ", 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) {//query is not available, user name can be used by users user = new user (); User.setusername (username); user.setuse Rpassword (password); User.save (Mainactivity.this, New Savelistener () {@Overridepublic void onsuccess () { Toast.maketext (Mainactivity.this, "registered successfully! Jump to the login 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, "username has been registered!) ", 3). Show ();}} @Overridepublic void OnError (int arg0, String arg1) {//TODO auto-generated Method stub}}})
3. Login 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 "> <Tabl Elayout android:id= "@+id/tllayout" android:layout_width= "match_parent" android:layout_height= "Wrap_co                Ntent "> <TableRow> <textview android:layout_width=" Wrap_content "             android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "User name:"/> <edittext android:id= "@+id/et_usernamelogin" android:layout_width= "Wrap_conte                                NT "android:layout_height=" Wrap_content "android:layout_weight=" 5 " Android:text= ""/> </TableRow> <TableRow> <textview Androi                D: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_we ight= "5" android:text= "/> </TableRow> </TableLayout> <button Androi D:id= "@+id/login" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android:la yout_below= "@+id/tllayout" android:text= "Login"/></relativelayout>


4. Login 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;import 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, "8f3ffb2658d8a3366a70a0b0ca0b71b2");//Initialize Bmobmloginname = (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.gettext (). toString (); final String password = Mloginpassword.gettext (). toString (); if (Username.equals ("") | | Password.equals ("")) {Toast.maketext ("") {login.this, "username or password cannot be empty! ", 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) {//cannot be queried, user name is available toast.maketext (Login.this, "username is wrong! ", 3). Show ();} else {if (userlist.get (0). Getuserpassword (). Equals (password)) {Toast.maketext (login.this, "Login successful! ", 3). Show (); Intent Intent = new Intent (); Intent.setclass (Login.this, Success.class); startactivity (Intent);} else {toast.maketext (login.this, "Password is wrong! ", 3). Show ();}}} @Overridepublic void OnError (int arg0, String arg1) {//TODO auto-generated Method stub}});}});}} 

5. Login successful. 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:o rientation= "vertical" >    <textview        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "        android:text=" login success! "/>    </LinearLayout>

6. Login 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= "/> <uses-permission android:name=" android.pe Rmission. INTERNET "/> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "/> <uses-permission an Droid: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:them E= "@style/apptheme" > <activity android:Name= "com.example.logintest.MainActivity" android:label= "@string/app_name" > <intent-filter&gt                ; <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.catego Ry. LAUNCHER "/> </intent-filter> </activity> <activity android:name=" Com.example.l Ogintest. Login "> </activity> <activity android:name=" com.example.logintest.Success "> </act Ivity> </application></manifest>

9. Documents:


10. Run:





Like a friend can pay attention to me! Thank you



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Simple Tutorial-the 23rd gun (based on Baas user registration and Login module implementation!) )

Related Article

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.