Android实现简洁的APP登录界面
程序员文章站
2022-08-02 15:02:40
今天需求要做一个所有app都有的登录界面,正好巩固一下我们之前学的基础布局知识。
先来看下效果图
1.布局的xml文件
今天需求要做一个所有app都有的登录界面,正好巩固一下我们之前学的基础布局知识。
先来看下效果图
1.布局的xml文件
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#2197db" > <imageview android:id="@+id/loginbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="40dp" android:src="@drawable/login_pic"/> <linearlayout android:id="@+id/input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/loginbutton" android:layout_marginleft="28dp" android:layout_marginright="28dp" android:background="#fff" android:orientation="vertical"> <linearlayout android:layout_width="fill_parent" android:layout_height="44dp" android:background="#fff" android:gravity="center_vertical" android:orientation="horizontal" > <edittext android:id="@+id/userid" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:imeoptions="actiondone" android:textsize="16sp" android:ems="10" android:hint="请输入用户名" > </edittext> <button android:id="@+id/button_bar" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginright="8dp" android:layout_marginleft="1dp" android:background="@drawable/login_input_arrow" /> </linearlayout> <view android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginleft="1.0px" android:layout_marginright="1.0px" android:background="#ffc0c3c4" /> <edittext android:id="@+id/pass" android:layout_width="fill_parent" android:layout_height="44.0dip" android:background="#00ffffff" android:gravity="center_vertical" android:inputtype="textpassword" android:maxlength="16" android:maxlines="1" android:textcolor="#ff1d1d1d" android:textcolorhint="#ff666666" android:textsize="16.0sp" android:hint="请输入密码" /> </linearlayout> <button android:id="@+id/loginbtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/input" android:layout_margintop="10dp" android:background="#3aadfd" android:text="登 录" android:textcolor="#ffffff" android:textsize="18dp" android:layout_centerhorizontal="true" android:layout_marginleft="28dp" android:layout_marginright="28dp"/> <textview android:text="" android:layout_width="wrap_content" android:layout_below="@+id/loginbtn" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:id="@+id/prompttext" android:textcolor="#ff0000" android:layout_margintop="10dp" android:textsize="18sp"/> </relativelayout>
2.java部分代码
public class loginactivity extends activity implements view.onclicklistener{ private static final string tag = "login"; button loginbtn = null; edittext useridet = null; edittext passet = null; textview prompttext = null; @override super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); loginbtn = (button) findviewbyid(r.id.loginbtn); loginbtn.setonclicklistener(this); useridet = (edittext) findviewbyid(r.id.userid); passet = (edittext) findviewbyid(r.id.pass); prompttext = (textview) findviewbyid(r.id.prompttext); okhttpclient okhttpclient = new okhttpclient.builder() .connecttimeout(10000l, timeunit.milliseconds) .readtimeout(10000l, timeunit.milliseconds) .build(); okhttputils.initclient(okhttpclient); @override public void onclick(view v) { string userid = useridet.gettext().tostring().trim(); string pass = passet.gettext().tostring().trim(); if(userid.equals("")){ prompttext.settext(r.string.useriderror); return ; } if(pass.equals("")){ prompttext.settext(r.string.passerror); return ; } webconstant.digest = ("basic " + new string(base64.encode((userid + ':' + pass).getbytes(), base64.default))).replace("\n", ""); string url = webconstant.requestpath+"/users/" + userid+"?getall=true"; okhttputils.get() .url(url).addheader("authorization", webconstant.digest).addheader("accept-language","zh-cn") .build().execute(new callback() { @override public string parsenetworkresponse(response response, int id) throws exception { string string = response.body().string(); jsonobject jsonobj = new jsonobject(string); if(jsonobj.get("username")!=null){ webconstant.userid = (string)jsonobj.get("userid"); webconstant.username = (string)jsonobj.get("username"); return (string) jsonobj.get("username"); } return null; } @override public void onerror(call call, exception e, int id) { webconstant.digest = null; prompttext.settext(r.string.loginerror); log.i(tag,e.getmessage()); e.printstacktrace(); } @override public void onresponse(object response, int id) { prompttext.settext(r.string.loginsuccess+" "+response); intent intent = new intent(); loginactivity.this.setresult(webconstant.result_ok, intent); loginactivity.this.finish(); } }); } }
简单的登录,用户名密码验证。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android中利用App实现消息推送机制的代码
-
php-android APP 实现微信登录,并同PHP服务器建立连接通信 (悬赏10C币)
-
Android仿拉手网团购App我的收藏界面实例代码
-
Android开发实现根据包名判断App运行状态的方法
-
Android布局管理器-使用LinearLayout实现简单的登录窗口布局
-
AndroidStudio制作登录和注册功能的实现,界面的布局介绍
-
android 实现APP中改变头像图片的实例代码
-
mysql-android app登录注册的问题
-
Android实现背景可滑动登录界面 (不压缩背景弹出键盘)
-
MFC登录界面实现类似QQ的功能(详细步骤及代码)