接着上两篇文章。我们基于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. Hadoop全分布式模式安装

    一.准备 1.准备至少三台linux服务器,并安装JDK 关闭防火墙如下 systemctl stop firewalld.service systemctl disable firewalld.se ...

  2. Linux问题故障定位

    CPU 针对应用程序,通常关注的是内核CPU调度器功能和性能. 线程的状态分析主要是分析线程的时间用在什么地方,而线程状态的分类一般分为: a. on-CPU:执行中,执行中的时间通常又分为用户态时间 ...

  3. iOS-----5分钟学会枚举的正确使用姿势-Enumeration宏

    前言 Enum,枚举,相信大部分编程语言都有对应的枚举类型,功能可能有多有少,但是枚举最核心的功能是 “规范的定义代码中的状态.状态码.选项”. 状态.状态码.选项 什么是状态:同时只能出现一个值(状 ...

  4. UVa——400Unix ls(字典序文本处理输出iomanip)

    Unix ls Time Limit:                                                        3000MS                    ...

  5. ubuntu14.04 安装 tensorflow9.0

    ubuntu14.04 安装 tensorflow9.0 文章目录 ubuntu14.04 安装 tensorflow9.0 安装pip(笔者的版本为9.0) 仅使用 CPU 的版本的tensorfl ...

  6. Element 'plugin' cannot have character [children], because the type's content type is element-only

    原因是你复制的时候,带了一些特殊符号. 解决方案: 将那一串代码复制到notpad++ 或者文本上面,再复制到你的编译器里面,就可以解决问题了

  7. python -- DNS处理模块dnspython

    简介 dnspython – 是python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验 安装dnspython pip install dnspython 使用 常见 ...

  8. 主机ping不通虚拟机,但是虚拟机能ping通主机

    一.虚拟机网络连接方式选择Nat 二. 关闭Linux防火墙命令:service iptables stop / service firewalld stop 查看Linux防火墙状态命令:servi ...

  9. 马士兵hadoop第五课:java开发Map/Reduce(转)

    马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...

  10. UVA 116 Unidirectional TSP(DP最短路字典序)

    Description    Unidirectional TSP  Background Problems that require minimum paths through some domai ...