今天需求要做一个所有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
# 登录界面
# Android Studio实现登录界面功能
# Android实现登录界面的注册功能
# Android登录界面的实现代码分享
# Android实现注册登录界面的实例代码
# Android设计登录界面、找回密码、注册功能
# 功能强大的登录界面Android实现代码
# Android属性动画实现炫酷的登录界面
# Android开发实例之登录界面的实现
# Android QQ登录界面绘制代码
# Android Studio实现简易登录界面制作
# 请输入
# 要做
# 大家多多
# actionDone
# textSize
# imeOptions
# layout_weight
# null
# hint
# finish
# sp
# ems
# userId
# orientation
# vertical
# fff
# layout_marginRight
# fill_parent
# horizontal
# EditText
相关文章:
如何通过VPS搭建网站快速盈利?
创业网站制作流程,创业网站可靠吗?
如何通过西部数码建站助手快速创建专业网站?
大连网站设计制作招聘信息,大连投诉网站有哪些?
如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何在Windows虚拟主机上快速搭建网站?
建站之星备案是否影响网站上线时间?
为什么Go需要go mod文件_Go go mod文件作用说明
,怎么用自己头像做动态表情包?
网站制作公司广州有几家,广州尚艺美发学校网站是多少?
北京制作网站的公司,北京铁路集团官方网站?
,柠檬视频怎样兑换vip?
网页设计与网站制作内容,怎样注册网站?
建站之星ASP如何实现CMS高效搭建与安全管理?
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
建站与域名管理如何高效结合?
建站主机服务器选型指南与性能优化方案解析
高防网站服务器:DDoS防御与BGP线路的AI智能防护方案
如何登录建站主机?访问步骤全解析
如何选择可靠的免备案建站服务器?
linux top下的 minerd 木马清除方法
深圳网站制作培训,深圳哪些招聘网站比较好?
头像制作网站在线制作软件,dw网页背景图像怎么设置?
教程网站设计制作软件,怎么创建自己的一个网站?
公司网站设计制作厂家,怎么创建自己的一个网站?
如何将凡科建站内容保存为本地文件?
建站之星如何实现网站加密操作?
企业微网站怎么做,公司网站和公众号有什么区别?
制作营销网站公司,淘特是干什么用的?
如何注册花生壳免费域名并搭建个人网站?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
建站之星3.0如何解决常见操作问题?
建站之星CMS五站合一模板配置与SEO优化指南
详解jQuery停止动画——stop()方法的使用
高性能网站服务器配置指南:安全稳定与高效建站核心方案
上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?
齐河建站公司:营销型网站建设与SEO优化双核驱动策略
详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】
桂林网站制作公司有哪些,桂林马拉松怎么报名?
如何选择美橙互联多站合一建站方案?
宁波自助建站系统如何快速打造专业企业网站?
如何在Tomcat中配置并部署网站项目?
宝塔建站无法访问?如何排查配置与端口问题?
制作网站的基本流程,设计网站的软件是什么?
建站之星如何快速解决建站难题?
如何在服务器上配置二级域名建站?
如何确保FTP站点访问权限与数据传输安全?
建站OpenVZ教程与优化策略:配置指南与性能提升
如何通过商城免费建站系统源码自定义网站主题?
*请认真填写需求信息,我们会在24小时内与您取得联系。