android登录demo,Xamarin.Android再体验之简单的登陆Demo

1、前言

在空闲之余,学学新东西android

2、服务端的代码编写与部署

这里采起的方式是MVC+EF返回Json数据,(原本是想用Nancy来实现的,想一想电脑太卡就不开多个虚拟机了,用用IIS部署也好)git

主要是接受客户端的登录请求,服务器端返回请求的结果github

这里的内容比较简单不在啰嗦,直接上代码了:json

1 usingSystem.Linq;2 usingSystem.Web.Mvc;3 namespaceCatcher.AndroidDemo.EasyLogOn.Service.Controllers4 {5 public classUserController : Controller6 {7 public ActionResult LogOn(string userName, stringuserPwd)8 {9 bool result =IsAuth(userName,userPwd);10 ReturnModel m = newReturnModel();11 if(result)12 {13 m.Code = "00000";14 m.Msg = "Success";15 }16 else

17 {18 m.Code = "00001";19 m.Msg = "Failure";20 }21 returnJson(m, JsonRequestBehavior.AllowGet);22 }23 public bool IsAuth(string name, stringpwd)24 {25 using (Models.DBDemo db = newModels.DBDemo())26 {27 int count = db.UserInfo.Count(u=>u.UserName==name&&u.UPassword==pwd);28 return count == 1 ? true : false;29 }30 }31 }32 public classReturnModel33 {34 public string Code { get; set; }35 public string Msg { get; set; }36 }37 }

发布,测试一下是否可行服务器

a5eccbf70a5f08dd57c0be81039dd208.png

OK网络

3、客户端(Android)的编码实现

既然是登陆,确定有两个文本框和一个登录按钮啦~app

登陆以后又要有什么呢,显示一下欢迎就够了,放一个TextViewide

下面就来布局一下(左边是Main.axml,右边是User.axml)布局

d26a69384fb0e359e438d55ae1e2785d.png      

6fd0baccd5da72dd7f80a104cc5821a6.png

具体的布局代码以下:测试

Main.axml

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:orientation="vertical"

4 android:layout_width="fill_parent"

5 android:layout_height="fill_parent">

6

8 android:minWidth="25px"

9 android:minHeight="80px"

10 android:layout_marginTop="20dp"

11 android:layout_width="match_parent"

12 android:layout_height="wrap_content"

13 android:id="@+id/linearLayoutForName">

14

16 android:layout_width="81.5dp"

17 android:layout_height="match_parent"

18 android:id="@+id/textViewName"

19 android:textAllCaps="true"

20 android:textSize="25dp"

21 android:textStyle="bold"

22 android:gravity="center" />

23

25 android:layout_height="match_parent"

26 android:id="@+id/txtName" />

27

28

30 android:minWidth="25px"

31 android:minHeight="80px"

32 android:layout_width="match_parent"

33 android:layout_height="wrap_content"

34 android:layout_below="@id/linearLayoutForName"

35 android:layout_marginTop="20dp"

36 android:id="@+id/linearLayoutForPwd">

37

39 android:layout_width="81.5dp"

40 android:layout_height="match_parent"

41 android:id="@+id/textViewPwd"

42 android:textAllCaps="true"

43 android:textSize="25dp"

44 android:textStyle="bold"

45 android:gravity="center" />

46

48 android:layout_height="match_parent"

49 android:id="@+id/txtPwd"

50 android:inputType="textPassword" />

51

52

54 android:layout_width="match_parent"

55 android:layout_height="wrap_content"

56 android:layout_marginTop="20dp"

57 android:layout_below="@id/linearLayoutForPwd"

58 android:id="@+id/btnLogin"

59 android:textAllCaps="true"

60 android:textSize="25dp"

61 android:textStyle="bold"

62 android:gravity="center" />

63

User.axml

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:orientation="vertical"

4 android:layout_width="match_parent"

5 android:layout_height="match_parent"

6 android:minWidth="25px"

7 android:minHeight="25px">

8

10 android:layout_width="match_parent"

11 android:layout_height="wrap_content"

12 android:id="@+id/tvInfo"

13 android:minHeight="60dp"

14 android:gravity="center"

15 android:textSize="20dp" />

16

主要是是相对布局与线性布局的结合

布局好了,就该编写实现代码了!!

MainActivity.cs

主要就是接收用户的输入,进行判断和校验,经过的就跳转到下一页面。这里面还用到了一点网络请求。

由于是演示,因此是一大堆代码。。。

里面有用到json的解析,用的是Newtonsoft.Joson,固然,写法不规范,不要吐槽。

1 usingAndroid.App;2 usingAndroid.Content;3 usingAndroid.OS;4 usingAndroid.Widget;5 usingNewtonsoft.Json;6 usingSystem;7 usingSystem.IO;8 usingSystem.Net;9 namespaceCatcher.AndroidDemo.EasyLogOn10 {11 [Activity(Label = "简单的登陆Demo", MainLauncher = true, Icon = "@drawable/icon")]12 public classMainActivity : Activity13 {14 protected override voidOnCreate(Bundle bundle)15 {16 base.OnCreate(bundle);17 //Set our view from the "main" layout resource

18 SetContentView(Resource.Layout.Main);19 EditText myName = FindViewById(Resource.Id.txtName);20 EditText myPwd = FindViewById(Resource.Id.txtPwd);21 Button login = FindViewById(Resource.Id.btnLogin);22 login.Click += delegate

23 {24 string name =myName.Text;25 string pwd =myPwd.Text;26 if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd))27 {28 Toast.MakeText(this, "请输入用户名和密码!!", ToastLength.Long).Show();29 return;30 }31 else

32 {33 string loginUrl = string.Format("http://192.168.1.102:8077/User/LogOn?userName={0}&userPwd={1}", name, pwd);34 var httpReq = (HttpWebRequest)HttpWebRequest.Create(newUri(loginUrl));35 var httpRes =(HttpWebResponse)httpReq.GetResponse();36 if (httpRes.StatusCode ==HttpStatusCode.OK)37 {38 string result = newStreamReader(httpRes.GetResponseStream()).ReadToEnd();39 result = result.Replace("\"", "'");40 ReturnModel s = JsonConvert.DeserializeObject(result);41 if (s.Code == "00000")42 {43 var intent = new Intent(this, typeof(UserActivity));44 intent.PutExtra("name", name);45 StartActivity(intent);46 }47 else

48 {49 Toast.MakeText(this, "用户名或密码不正确!!", ToastLength.Long).Show();50 return;51 }52 }53 }54 };55 }56 }57 public classReturnModel58 {59 public string Code { get; set; }60 public string Msg { get; set; }61 }62 }

下面就是跳转以后的页面了,就是从MainActivity传过来的用户名显示在TextView那里。

1 usingAndroid.App;2 usingAndroid.Content;3 usingAndroid.OS;4 usingAndroid.Widget;5 namespaceCatcher.AndroidDemo.EasyLogOn6 {7 [Activity(Label = "用户首页")]8 public classUserActivity : Activity9 {10 protected override voidOnCreate(Bundle savedInstanceState)11 {12 base.OnCreate(savedInstanceState);13 //Create your application here

14 SetContentView(Resource.Layout.User);15 TextView info = FindViewById(Resource.Id.tvInfo);16 string name = Intent.GetStringExtra("name");17 info.Text = name + "欢迎您的到来!";18 }19 }20 }

而后就OK了,是否是也很Easy呢。

下面来看看效果

什么都不输入的时候,输入错误的时候,输入正确的时候

fbead954e579244ae63b7c23fa301cc0.png    

b6d04b47e0fd215ea262f0b2c41313c1.png     

1da56edfac70854ee9086a6b54c4edc0.png

若是想生成apk文件的话,须要将模式调整为Release模式!!!

c2ed4a7b8430f81475f2178160bb8ba4.png

4、总结对比

跟原生的Android开发(Java)相比,有不少类似的地方也有不少细节区别,适应就好。

作这个Demo的时候,编写界面的时候貌似没发现有智能提示,不知道有没有处理的好办法!

须要注意的是,这个Demo在数据的传输过程当中并无进行加解密的处理,这是不可取的!

其余的话,就两个字的感受,方便。

最后附上这个Demo的代码:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值