简单登录注册页面(初学者),可以和后台交互
客户端:
客户端实现了简单的登录、注册功能、还可保存输入的用户名和密码。使用的POST请求实现和后台服务端的交互。不多说,上代码:
客户端布局文件(activity_main.xml):

<EditTextandroid:id="@+id/id"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="78dp"android:ems="10"android:inputType="textPersonName"android:hint="用户名" /><EditTextandroid:id="@+id/password"android:layout_width="wrap_content"android:layout_height="wrap_content"    android:layout_alignRight="@+id/id"android:layout_below="@+id/id"android:layout_marginTop="28dp"android:ems="10"android:inputType="textPassword"android:hint="密码"/><CheckBox android:id="@+id/check"android:text="记住用户名和密码"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/password"android:layout_alignLeft="@+id/password"/><Buttonandroid:id="@+id/register"android:text="注册"android:layout_marginTop="40dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/password"android:layout_alignLeft="@+id/password"/><Buttonandroid:id="@+id/login"android:text="登录"android:layout_marginTop="40dp"android:layout_below="@id/password"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignRight="@id/password"/>

MainActivity.java:

public class MainActivity extends Activity implements OnClickListener{private EditText id;private EditText password;private Button login;private Button register;private CheckBox cb;private String username;private String pwd;private SharedPreferences sp;private String result;private int RequestCode = 1;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);id = (EditText) findViewById(R.id.id);password = (EditText) findViewById(R.id.password);login = (Button) findViewById(R.id.login);//register.setOnClickListener(this);login.setOnClickListener(this);register = (Button) findViewById(R.id.register);   register.setOnClickListener(this);cb = (CheckBox) findViewById(R.id.check);sp = getSharedPreferences("config", 0);//将数据显示到UI控件//把config.xml文件中的数据取出来显示到EditText控件中//如果没找到key键对应的值,会返回第二个默认的值String username = sp.getString("username", "");String password = sp.getString("password", "");id.setText(username);this.password.setText(password);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==1&&resultCode==2){id.setText(data.getStringExtra("id"));password.setText(data.getStringExtra("password"));}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.login:username = id.getText().toString().trim();pwd = password.getText().toString().trim();if(cb.isChecked()){//判断是否勾选
//          //使用sharePreferences区保存数据 拿到sp实例
//          //参数:  name生成xml文件
//          //mode 模式
//
//          //获取sp的编辑器Editor editor=sp.edit();
//          //存储用户输入的数据editor.putString("username", username);editor.putString("password", pwd);
//          //提交editoreditor.commit();}if(TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){Toast.makeText(getApplicationContext(), "账号或密码不能为空", 1).show();}else{new Thread(){public void run(){try {//设置路径//设置路径String path="http://192.168.227.1:8080/MyWebsite/androidlogin.do?id="+username+"&password="+pwd+"";//创建URL对象URL url=new URL(path);//创建一个HttpURLConnection对象HttpURLConnection conn=(HttpURLConnection) url.openConnection();//设置请求方法conn.setRequestMethod("POST");//设置请求超时时间conn.setReadTimeout(5000);conn.setConnectTimeout(5000);//Post方式不能设置缓存,需要手动设置conn.setUseCaches(false);//设置我们的请求数据String data="id="+username+"&password="+pwd;conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//使用的是表单请求类型conn.setRequestProperty("Content-Length", data.length()+"");conn.setDoOutput(true);conn.getOutputStream().write(data.getBytes());
//                          //连接
//                          conn.connect();
//                          //获取一个输出流
//                          OutputStream out=conn.getOutputStream();
//                          out.write(data.getBytes());//获取服务器返回的状态吗int code=conn.getResponseCode();if(code==200){//获取服务器返回的输入流对象InputStream in= conn.getInputStream();result = StringTools.readStream(in);//更新UIrunOnUiThread(new Runnable() {@Overridepublic void run() {if(result.equals("success"))// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "登录成功", 1).show();else{Toast.makeText(getApplicationContext(), "登录失败", 1).show();}}});}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}};}.start();}break;case R.id.register://跳转到登录页面Intent intent=new Intent(this,RegisterActivity.class);startActivityForResult(intent,RequestCode);break;default:break;}        }
}

注册页面布局文件(register.xml):

<EditTextandroid:id="@+id/id_edit"android:hint="用户名"android:layout_marginTop="50dp"android:layout_width="match_parent"android:layout_height="wrap_content" />
<EditTextandroid:id="@+id/password_edit"android:hint="密码"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" /> <EditTextandroid:id="@+id/password_edit_1"android:hint="确认密码"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" />
<EditTextandroid:id="@+id/email_edit"android:hint="邮箱"android:layout_below="@+id/password_edit_1"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/register_do"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="click1"android:text="注册" />

注册页面逻辑(RegisterActivity.java):
public class RegisterActivity extends Activity implements OnClickListener{
private Button register;
private EditText id;
private EditText pwd_1;
private EditText pwd_2;
private EditText email;
private String result;
private String username;
private String pwd1;
private String e_mail;
private String pwd2;
private int ResultCode=2;

protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.register); register = (Button) findViewById(R.id.register_do);register.setOnClickListener(this);id = (EditText) findViewById(R.id.id_edit);pwd_1 = (EditText) findViewById(R.id.password_edit);pwd_2 = (EditText) findViewById(R.id.password_edit_1);email = (EditText) findViewById(R.id.email_edit);
}@Override
public void onClick(final View v) {new Thread(){public void run() {switch (v.getId()) {case R.id.register_do:username = id.getText().toString().trim();e_mail = email.getText().toString().trim();pwd1 = pwd_1.getText().toString().trim();pwd2 = pwd_2.getText().toString().trim();if(!pwd1.equals(pwd2)){runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "两次输入密码不一致,请重新输入!", 1).show();}});}else {try {//设置路径String path="http://192.168.227.1:8080/MyWebsite/androidregister.do?id="+username+"&password="+ pwd1+"&email="+e_mail+"";//创建URL对象URL url=new URL(path);//创建一个HttpURLconnection对象HttpURLConnection conn =(HttpURLConnection) url.openConnection();//设置请求方法conn.setRequestMethod("POST");//设置请求超时时间conn.setReadTimeout(5000);//conn.setConnectTimeout(5000);//Post方式不能设置缓存,需要手动设置//conn.setUseCaches(false);//准备要发送的数据String data ="id="+URLEncoder.encode(username,"utf-8")+"&password"+URLEncoder.encode(pwd1,"utf-8")+"&email"+URLEncoder.encode(e_mail,"utf-8");conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//使用的是表单请求类型conn.setRequestProperty("Content-Length", data.length()+"");conn.setDoOutput(true);//连接// conn.connect();//获得返回的状态码conn.getOutputStream().write(data.getBytes());int code=conn.getResponseCode();if(code==200){//获得一个文件的输入流InputStream inputStream= conn.getInputStream();result = StringTools.readStream(inputStream);//更新UIshowToast(result);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}break;default:break;}        };}.start();
}
public void showToast(final String content){runOnUiThread(new Runnable() {      @Overridepublic void run() {// TODO Auto-generated method stubif(result.equals("success")){Toast.makeText(getApplicationContext(), "注册成功", 1).show();Intent intent=new Intent();  intent.putExtra("id", username);intent.putExtra("password", pwd1);setResult(ResultCode, intent);finish();}      }});
}     }

工具类:将客户端发送的流数据转化为字符串(StringTools):

 public static String readStream(InputStream in)throws Exception{//将传进来的流信息转换为字符串//创建1字节输出流对象ByteArrayOutputStream outputStream=new ByteArrayOutputStream();//定义读取长度int len=0;//定义缓存区byte buffer[]=new byte[2014];//按照缓存区大小循环读取while((len=in.read(buffer))!=-1){outputStream.write(buffer, 0, len);}in.close();outputStream.close();//将字符串数据返回String content=new String(outputStream.toByteArray());return content;     }}

以上是客户端的所有内容,包含所有的页面和对应的逻辑。
服务端的内容:不具体贴出所有的代码,给予简单的介绍:
开发工具:Eclipse2018
数据库:MySQL数据库(数据库的创建,请跟具代码具体创建)
具体服务端代码:点击以下链接下载
服务端代码
提取密码:lncl

Android的一个登陆注册页面相关推荐

  1. JavaEE学习之jsp编写登陆注册页面

    JavaEE学习之jsp编写登陆注册页面 刚开始学习javaee,好多东西需要一点点积累.最近用jsp和简单的JavaScript写的登录注册界面,简单做一下记录. 准备–页面布局 登录和注册界面的H ...

  2. 图书管理系统之登陆注册页面布局(一)

    图书管理系统之登陆注册页面布局(一) 相关源码下载连接:https://download.csdn.net/download/baidu_39378193/85033291 前言,创建一个新的MVC项 ...

  3. jsp mysql登录注册实验报告_登陆注册页面实验报告.doc

    登陆注册页面实验报告 兰州理工大学 二.数据库设计 本系统采用mysql数据库,只有一个表:数据表userinfoinfo用来存储后台会员名称,密码和基本资料. 2.1用户信息表: 下面是用户信息表表 ...

  4. python 登陆注册页面练习

    文章目录 一.英雄联盟登陆 二.md5盐值登陆注册页面 一.英雄联盟登陆 import random while True:print("\t\t\t英雄联盟商城界面\n")pri ...

  5. html制作登陆注册页面

    html制作登陆注册页面 源代码如下 <!DOCTYPE html> <html lang="en"> <head><meta chars ...

  6. html登录页面用idea,利用IDEA怎么制作一个登录注册页面

    利用IDEA怎么制作一个登录注册页面 发布时间:2020-12-19 14:02:09 来源:亿速云 阅读:186 作者:Leah 利用IDEA怎么制作一个登录注册页面?很多新手对此不是很清楚,为了帮 ...

  7. 12月11日,12月12日登陆注册页面的进度

    12月11日晚 抽出时间读学姐给的登录注册页面代码,有不懂的地方就百度,基本搞清楚了点登陆注册页面的基本框架和元素的作用.重点学习了<input><button><for ...

  8. javaweb 登陆注册页面

    视图的数据修改,表中也修改 引用工具类用<%@ page import=""%> <%@ page import="java.util.Date&quo ...

  9. PHP实现简单登陆注册页面

    PS:个人学习记录 数据库设置: login.html: <!DOCTYPE html> <html><head><meta charset = " ...

  10. java web程序 上机考试做一个登陆注册程序

    大二期末 java web.用到数据库,jdbc.myeclipse实现用户的注册,登陆 并且不能出现500错误,用户不能重复注册.当用户任意点击时也不能出现500错误! 这里.我只写注册成功的页面. ...

最新文章

  1. Spring @bean冲突解决方案
  2. c++中的list用法
  3. 静观接入网易云信IM的秀品,如何在圣诞让她们疯狂剁手
  4. windows桌面待办事项_想在手机桌面上安装一个便利贴,下载什么便签软件好?
  5. android 对for循环进行优化
  6. python回调接口_三个案例带你了解python回调函数
  7. 自定义函数删除字母C语言,[编程入门]自定义函数之字符提取-题解(C语言代码)...
  8. 托管元数据(2)——托管元数据和搜索中的精简面板
  9. 家长又放心了一些!教育类App不能再干这些事了
  10. 使用entityframework操作sqlite数据库
  11. LIRe 源代码分析 1:整体结构
  12. css3 切换贞动画的效果,仿gif效果
  13. C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP
  14. 免费录屏软件有哪些?分享4个专业录屏软件
  15. 微信开放平台、微信公众平台和微信商户平台
  16. 5G牌照都发完了,那些传说中的5G手机Ready了吗?
  17. 从2018年全球半导体数据中看物联网芯片产业现状
  18. 微信小程序订阅信息之Java实现详解
  19. go-micro教程
  20. Regional 做题记录 (50/50)

热门文章

  1. 鸿蒙桌面设置教程,鸿蒙系统桌面怎么设置好看?好看的鸿蒙系统手机桌面设置布局推荐...
  2. echarts横向柱状图
  3. 数字IC设计入门(9)初识数字芯片验证
  4. 区块链掀起的认知革命!|筱静观察
  5. STC15单片机-PCB设计
  6. opencv 修改图片尺寸
  7. Jetpack DataStore 你总要了解一下吧?
  8. 数据库(表结构)设计技巧及注意事项
  9. Azure Tools---CAT(一)
  10. 车辆管理设备V系列JTT-808协议简介