Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)
Posted claireyuancy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)相关的知识,希望对你有一定的参考价值。
接着上两篇文章。我们基于Bmob提供的API实现用户登录功能。总体看一下代码。
1.注冊页面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="用户名:" /> <EditText android:id="@+id/et_username" android: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="密码:" /> <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="注冊" /> </RelativeLayout>
2.注冊页面.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; @Override protected 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.getText().toString(); if (username.equals("") || password.equals("")) { Toast.makeText(this, "用户名或密码不能为空!3.登录界面.xml:", 3).show(); } else { BmobQuery<User> query = new BmobQuery<User>();// 查询类 query.addWhereEqualTo("userName", username);// 查询条件 query.findObjects(MainActivity.this, new FindListener<User>() { @Override public void onSuccess(List<User> userlist) { if (userlist.size() == 0) {// 查询不到,用户名可用 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(); Intent intent = new Intent(); intent.setClass(MainActivity.this, Login.class); intent.putExtra("username", username); startActivity(intent); } @Override public void onFailure(int arg0, String arg1) { Toast.makeText(MainActivity.this, "注冊失败。", 3) .show(); } }); } else { Toast.makeText(MainActivity.this, "用户名已被注冊!
", 3).show(); } } @Override public void onError(int arg0, String arg1) { // TODO Auto-generated method stub } }); } } }
<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="用户名:" /> <EditText android:id="@+id/et_usernamelogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="5" android:text="" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="密码:" /> <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="登录" /> </RelativeLayout>
4.登录界面.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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); Bmob.initialize(this, "8f3ffb2658d8a3366a70a0b0ca0b71b2");// 初始化Bmob mLoginName = (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() { @Override public 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, "用户名或密码不能为空!", 3).show(); } else { BmobQuery<User> query = new BmobQuery<User>();// 查询类 query.addWhereEqualTo("userName", username);// 查询条件 query.findObjects(Login.this, new FindListener<User>() { @Override public void onSuccess(List<User> userlist) { if (userlist == null) {// 查询不到。用户名可用 Toast.makeText(Login.this, "用户名错误!", 3).show(); } else { if (userlist.get(0).getUserPassword() .equals(password)) { Toast.makeText(Login.this, "登录成功。", 3) .show(); Intent intent = new Intent(); intent.setClass(Login.this, Success.class); startActivity(intent); } else { Toast.makeText(Login.this, "密码错误!", 3) .show(); } } } @Override public void onError(int arg0, String arg1) { // TODO Auto-generated method stub } }); } } }); } }
5.登录成功.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="登录成功!"/> </LinearLayout>
6.登录成功.java:
package com.example.logintest; import android.app.Activity; import android.os.Bundle; public class Success extends Activity { @Override protected 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.配置文件:
<?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.文件截图:
10.执行截图:
喜欢的朋友能够关注我!
谢谢
以上是关于Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)的主要内容,如果未能解决你的问题,请参考以下文章
Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)
Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)