Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用
GitHub地址:https://github.com/1165863642/LoginDemo
直接贴代码<?xml version="1.0" encoding="utf-8"?<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp"
android:textStyle="bold"/> <EditText
android:id="@+id/et_user"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20sp"
android:textStyle="bold"/> <EditText
android:id="@+id/et_pass"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout> <CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="65dp"
android:layout_marginTop="10dp"
android:text="记住用户名"
android:textStyle="bold"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/> <Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="取消"/>
</LinearLayout>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp"
android:textStyle="bold"/> <EditText
android:id="@+id/et_user"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20sp"
android:textStyle="bold"/> <EditText
android:id="@+id/et_pass"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout> <CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="65dp"
android:layout_marginTop="10dp"
android:text="记住用户名"
android:textStyle="bold"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/> <Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
package com.example.a11658.logindemo; import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button btn_login, btn_cancel;
EditText et_user, et_pass;
CheckBox cb_remember;
SharedPreferences spf; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} //初始化控件
private void initView() {
spf = getSharedPreferences("test", MODE_PRIVATE);
//关联控件
btn_cancel = findViewById(R.id.btn_cancel);
btn_login = findViewById(R.id.btn_login);
et_pass = findViewById(R.id.et_pass);
et_user = findViewById(R.id.et_user);
cb_remember = findViewById(R.id.cb_remember);
et_user.setText(spf.getString("username", "111"));
//点击事件
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//登陆
//1.获取用户名密码
String username = et_user.getText().toString().trim();
String password = et_pass.getText().toString().trim();
//2.判断是否记住用户名
if (cb_remember.isChecked()) { //判断CheckBox选中状态
spf.edit().putString("username", username).commit();
} else {
spf.edit().clear().commit();
} //3.判断用户名密码是否正确
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
if (username.equals("user") && password.equals("pass")) {
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "用户名密码不正确", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show();
}
}
}); btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}); et_pass.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//文字改变前
Toast.makeText(MainActivity.this,"请输入", Toast.LENGTH_SHORT).show();
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//正在输入
Toast.makeText(MainActivity.this,"正在输入", Toast.LENGTH_SHORT).show();
} @Override
public void afterTextChanged(Editable s) {
//输入结束后
Toast.makeText(MainActivity.this,"输入结束后", Toast.LENGTH_SHORT).show();
}
});
}
}
效果图::
代码地址:https://github.com/1165863642/LoginDemo
涉及到的一些知识点 不懂的可以咨询我 扣:1165863642 共同学习
Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用的更多相关文章
- Android开发8:UI组件TextView,EditText,Button
版本:Android4.3 API18 学习整理:liuxinming TextView 概述 TextView直接继承了View(EditText.Button两个UI组件类的父类) TextVie ...
- Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用
也许大家遇到这样一个问题,有时我们根据业务需要在一个ViewGroup中动态的(程序运行过程中)添加View.例如添加Button,就需要给Button添加background.padding.mar ...
- Android Studio登陆界面+Button不变色问题
今日所学内容: 1.初始相对布局 2.AS登录界面 3.一个可以下载小图标的阿里的网站iconfont-阿里巴巴矢量图标库 用GitHub账号绑定就可以免费下载 4.取颜色工具ColorCop 遇到的 ...
- android入门系列- TextView EditText Button ImageView 的简单应用
第一篇原创,其实自己就是一菜鸟,简单分享点基本知识吧.希望能有所帮助吧. TextView EditText Button ImageView 这几个控件可能是Android开发中最常用.最基本的几个 ...
- Android简单登陆页面
布局: 线性布局+相对布局 日志打印: 利用LogCat和System.out.println打印观察. Onclick事件是采用过的第四种: 在配置文件中给Button添加点击时间 涉及知识: 通过 ...
- Android笔记-4-实现登陆页面并跳转和简单的注册页面
实现登陆页面并跳转和简单的注册页面 首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px; ...
- 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...
- Android UI 设计之 TextView EditText 组件属性方法最详细解析
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...
- Android 有关在ListView RecycleView 中使用EditText Checkbox的坑
这是一篇文字超多的博客,哈哈哈,废话自行过滤··· 遇到问题 在开发中我们常会在ListView , RecycleView 列表中添加EditText输入框,或者checkbox复选框. 复选框 ...
随机推荐
- [Swift]LeetCode843. 猜猜这个单词 | Guess the Word
This problem is an interactive problem new to the LeetCode platform. We are given a word list of uni ...
- GraphQL-前端开发的利剑与桥梁
GraphQL-前端开发的利剑与桥梁 基本概念 GraphQL GraphQL 是一种用于 API 的查询语言,由Facebook开发和开源,是使用基于类型系统来执行查询的服务端运行时(类型系统由你的 ...
- iOS模拟器使用
在iOS开发过程中一直都是使用模拟器进行调试,在模拟器上有很多不适应的地方,但是其实在模拟器上也有很多其他的功能,在本文中主要对模拟器的一些基本功能进行总结一下. 1 首先,我们了解一下模拟器中常用的 ...
- 极速搭建RTMP直播流服务器+webapp (vue) 简单实现直播效果
在尝试使用webRTC实现webapp直播失败后,转移思路开始另外寻找可行的解决方案.在网页上尝试使用webRTC实现视频的直播与看直播,在谷歌浏览器以及safari浏览器上测试是可行的.但是基于基座 ...
- 死磕 java集合之TreeMap源码分析(四)-内含彩蛋
欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. 二叉树的遍历 我们知道二叉查找树的遍历有前序遍历.中序遍历.后序遍历. (1)前序遍历,先遍历 ...
- C#版[击败98.85%的提交] - Leetcode717. 1比特与2比特字符 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 封装cookie设置和获取的简易方法
(function() { var tool = { expires: "expires", // 过期时间expires path: "path", // 路 ...
- Chapter 5 Blood Type——18
"The Red Cross is having a blood drive in Port Angeles next weekend, so I thought you should al ...
- 精读《Serverless 给前端带来了什么》
1. 引言 Serverless 是一种 "无服务器架构",让用户无需关心程序运行环境.资源及数量,只要将精力 Focus 到业务逻辑上的技术. 现在公司已经实现 DevOps 化 ...
- python学习第三讲,python基础语法之注释,算数运算符,变量.
目录 python学习第三讲,python基础语法之注释,算数运算符,变量. 一丶python中的基础语法,注释,算数运算符,变量 1.python中的注释 2.python中的运算符. 3.pyth ...