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).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的用户注冊和登录模块实现!)的更多相关文章
- Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)
接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...
- Android实战简易教程-第十三枪(五大布局研究)
我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...
- Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)
接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...
- Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)
上一篇我们介绍了怎样由uri转换成String ,本文就用到了上篇文章的方法.以下我们介绍一下怎样设置头像后将头像图片上传到云端的方法,本文基于Bmob提供的服务. 看一下代码:(布局文件和前两篇文章 ...
- Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)
上一节我们实现了数据表的载入,可是,当数据表数据非常多时.我们就要考虑数据的分页.这里我们选用了PullToRefreshListView控件,先看一下该控件的说明: 效果图: ...
- Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)
1.头部布局文件top.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- Android实战简易教程-第二十八枪(Uri转String型实例)
接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...
- Android实战简易教程-第十枪(画廊组件Gallery有用研究)
Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现. 一.实现Gallery 1.布局文件非常easy: <?xml version="1.0" encoding ...
- Android实战简易教程-第四十枪(窃听风云之短信监听)
近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...
随机推荐
- URAL 1099 Work scheduling 一般图的最大匹配 带花树算法(模板)
R - Work scheduling Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 【bzoj3207】花神的嘲讽计划Ⅰ Hash+STL-map+莫队算法
题目描述 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天DJ在给吾等众蒟 ...
- C# Settings.settings的用处
1.定义 在Settings.settings文件中定义配置字段.把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改.可以使用数据网格视图,很方便: 2.读取配置值 t ...
- BZOJ4316 小C的独立集 【仙人掌】
题目 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. 这不,小C让小D去求一个无向图的最大独立集,通俗地讲就是:在无向图中选出若干个点,这些点互相没有边连接,并使取出的点尽量多. ...
- xsy 1845 - GCD
from NOIP2016模拟题34 Description 给定一个长度\(n\le 10^6\)的序列, 给定\(A, B\) 给出一个序列,要求你通过如下两个操作使得序列中所有数的最大公约数大于 ...
- poj 1410 Intersection 线段相交
题目链接 题意 判断线段和矩形是否有交点(矩形的范围是四条边及内部). 思路 判断线段和矩形的四条边有无交点 && 线段是否在矩形内. 注意第二个条件. Code #include & ...
- 标准C程序设计七---73
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Mac下Android SDK更新不了的解决办法
在hosts文件中加入: 203.208.46.146 dl.google.com 203.208.46.146 dl-ssl.google.com
- php--strlen()与mb_strlen的作用与区别
在PHP中,strlen与mb_strlen是求字符串长度的函数PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得 ...
- LeetCode OJ——Longest Valid Parentheses
http://oj.leetcode.com/problems/longest-valid-parentheses/ 最大括号匹配长度,括号是可以嵌套的 #include <string> ...