接着上两篇文章。我们基于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).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 }
}); } } }

3.登录界面.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="用户名:" /> <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的用户注冊和登录模块实现!)的更多相关文章

  1. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  2. Android实战简易教程-第十三枪(五大布局研究)

    我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...

  3. Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)

    接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...

  4. Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)

    上一篇我们介绍了怎样由uri转换成String ,本文就用到了上篇文章的方法.以下我们介绍一下怎样设置头像后将头像图片上传到云端的方法,本文基于Bmob提供的服务. 看一下代码:(布局文件和前两篇文章 ...

  5. Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)

    上一节我们实现了数据表的载入,可是,当数据表数据非常多时.我们就要考虑数据的分页.这里我们选用了PullToRefreshListView控件,先看一下该控件的说明: 效果图:            ...

  6. Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)

    1.头部布局文件top.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  7. Android实战简易教程-第二十八枪(Uri转String型实例)

    接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...

  8. Android实战简易教程-第十枪(画廊组件Gallery有用研究)

    Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现. 一.实现Gallery 1.布局文件非常easy: <?xml version="1.0" encoding ...

  9. Android实战简易教程-第四十枪(窃听风云之短信监听)

    近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...

随机推荐

  1. ruby操作mysql

    require "win32ole" require 'pathname' require 'mysql2' excel = WIN32OLE.new('excel.applica ...

  2. cronolog切割apache和tomcat日志

    cronolog切割apache和tomcat日志 http://cronolog.org tar zxvf cronolog-1.6.2.tar.gzcd cronolog-1.6.2./confi ...

  3. Linux下安装Mysql出现的常见问题以及解决办法

     1.安装时候出现 warning: mysql-community-server-5.7.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ...

  4. 内存分配(new/delete,malloc/free,allocator,内存池)

    以下来源http://www.cnblogs.com/JCSU/articles/1051826.html 程序员们经常编写内存管理程序,往往提心吊胆.如果不想触雷,唯一的解决办法就是发现所有潜伏的地 ...

  5. 【THUWC2017】在美妙的数学王国中畅游(bzoj5020)

    我数学是真的菜!! 清华光用数学知识就把我吊起来打,我还是太菜了 题解 如果每座城市的 $f$ 都是 $3$,维护一下树的路径上的 $\sum a,\space \sum b$ 即可. 其实就是维护一 ...

  6. Java Socket IO(BIO、NIO)

    总结下Java socket IO.首先是各种IO的定义,这个定义似乎也是众说纷纭.我按照stackoverflow上面的解释: IO有两种分法:按照阻塞或者按照同步.按照阻塞,有阻塞IO和非阻塞IO ...

  7. python基于SQLAlchemy的DBtools

    新版,只创建一次线程池 # -*- coding: utf-8 -*- from sqlalchemy import create_engine from sqlalchemy.orm import ...

  8. 自己关于Django的一些实践

    一 render() redirect() HttpResponse() 响应 是个什么东西 def login(request): if request.method=='POST': userna ...

  9. python算法与数据结构-顺序表(37)

    1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...

  10. php set_time_limit(0) 设置程序执行时间的函数

    一个简单的例子,在网页里显示1500条语句,如果未设置失效时间,则程序执行到791时结束了,如果把 set_time_limit(0); 前的注释符//去除,则程序直到1才结束.   set_time ...