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提供的后台 ...
随机推荐
- javascript学习笔记 - 变量、作用域和内存问题
一 垃圾收集 javascript具有自动垃圾收集机制.由垃圾收集机制标找出不再使用的变量.按照固定间隔的时间进行销毁,释放内存. 1.找出不再使用的变量的方法,如下: 1-1.标记清除 垃圾回收器 ...
- 【Luogu】P3155叶子的染色(树形DP)
题目链接 树形DP水题qwq. 设f[i][j]是以i为根的子树,染成j色,且满足内部需求的最少染色节点数. 设to是x的子节点,那么状态转移方程如此设计: 1.f[i][0] 这个状态表示i不染色, ...
- 数组快速生成range的方法
//生成[item1-item9]数组 Array(9).join(0).split('').map((item,index) => 'item' + (index+1)) //生成20个对象的 ...
- 欧拉回路 & 欧拉路径
欧拉路径 & 欧拉回路 概念 欧拉路径: 如果图 G 种的一条路径包括所有的边,且仅通过一次的路径. 欧拉回路: 能回到起点的欧拉路径. 混合图: 既有无向边又有无向边的图. 判定 无向图 一 ...
- FZOJ 2102 Solve equation
...
- 【CF696B】Puzzles(树形DP,期望)
题意:n 个节点的树,初始位置为 1 号节点,初始时间为 1.每次随机地走向任何一个没有走过的子树并且令时间 +1求问走到每一个点时的时间的期望值 思路:比较少见的一道自顶向下的树形DP dp[i]表 ...
- javascript事件捕获机制,dom tree
$(document,"a").on("click",function(){alert(2);return false;}); $("<a> ...
- 30深入理解C指针之---字符串和数组
一.字符串与数组 1.定义:使用字符数组表示字符串 2.特征: 1).可以直接使用字符串字面量初始化字符数组 2).声明后,赋值就只能使用字符串操作函数strcpy函数赋值 3).可以使用数组的一一赋 ...
- AC日记——小木棍【数据加强版】 洛谷 P1120
题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍的长度,编 ...
- VsCode搭建Java开发环境
1:在 Visual Studio Code 中打开扩展视图(Ctrl+Shift+X),输入关键词java.spring分别下载Java开发插件包和springboot插件包 2:配置参数 点击设置 ...