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"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:"android:textSize="20sp"android:textStyle="bold"/><EditTextandroid:id="@+id/et_user"android:layout_width="200dp"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码:"android:textSize="20sp"android:textStyle="bold"/><EditTextandroid:id="@+id/et_pass"android:layout_width="200dp"android:layout_height="wrap_content"/></LinearLayout><CheckBoxandroid: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"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="登陆"/><Buttonandroid: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"?> <LinearLayoutxmlns: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"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:"android:textSize="20sp"android:textStyle="bold"/><EditTextandroid:id="@+id/et_user"android:layout_width="200dp"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码:"android:textSize="20sp"android:textStyle="bold"/><EditTextandroid:id="@+id/et_pass"android:layout_width="200dp"android:layout_height="wrap_content"/></LinearLayout><CheckBoxandroid: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"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="登陆"/><Buttonandroid:id="@+id/btn_cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:text="取消"/></LinearLayout> </LinearLayout>
1 package com.example.a11658.logindemo; 2 3 import android.content.SharedPreferences; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.text.Editable; 7 import android.text.TextUtils; 8 import android.text.TextWatcher; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.CheckBox; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 15 public class MainActivity extends AppCompatActivity { 16 17 Button btn_login, btn_cancel; 18 EditText et_user, et_pass; 19 CheckBox cb_remember; 20 SharedPreferences spf; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 initView(); 27 } 28 29 //初始化控件 30 private void initView() { 31 spf = getSharedPreferences("test", MODE_PRIVATE); 32 //关联控件 33 btn_cancel = findViewById(R.id.btn_cancel); 34 btn_login = findViewById(R.id.btn_login); 35 et_pass = findViewById(R.id.et_pass); 36 et_user = findViewById(R.id.et_user); 37 cb_remember = findViewById(R.id.cb_remember); 38 et_user.setText(spf.getString("username", "111")); 39 //点击事件 40 btn_login.setOnClickListener(new View.OnClickListener() { 41 @Override 42 public void onClick(View v) { 43 //登陆 44 //1.获取用户名密码 45 String username = et_user.getText().toString().trim(); 46 String password = et_pass.getText().toString().trim(); 47 //2.判断是否记住用户名 48 if (cb_remember.isChecked()) { //判断CheckBox选中状态 49 spf.edit().putString("username", username).commit(); 50 } else { 51 spf.edit().clear().commit(); 52 } 53 54 //3.判断用户名密码是否正确 55 if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) { 56 if (username.equals("user") && password.equals("pass")) { 57 Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); 58 } else { 59 Toast.makeText(MainActivity.this, "用户名密码不正确", Toast.LENGTH_SHORT).show(); 60 } 61 } else { 62 Toast.makeText(MainActivity.this, "输入框不能为空", Toast.LENGTH_SHORT).show(); 63 } 64 } 65 }); 66 67 btn_cancel.setOnClickListener(new View.OnClickListener() { 68 @Override 69 public void onClick(View v) { 70 finish(); 71 } 72 }); 73 74 et_pass.addTextChangedListener(new TextWatcher() { 75 @Override 76 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 77 //文字改变前 78 Toast.makeText(MainActivity.this,"请输入", Toast.LENGTH_SHORT).show(); 79 } 80 81 @Override 82 public void onTextChanged(CharSequence s, int start, int before, int count) { 83 //正在输入 84 Toast.makeText(MainActivity.this,"正在输入", Toast.LENGTH_SHORT).show(); 85 } 86 87 @Override 88 public void afterTextChanged(Editable s) { 89 //输入结束后 90 Toast.makeText(MainActivity.this,"输入结束后", Toast.LENGTH_SHORT).show(); 91 } 92 }); 93 } 94 }
效果图::
Demo点击
涉及到的一些知识点 不懂的可以咨询我 扣:1165863642 共同学习
转载于:https://www.cnblogs.com/MrChen-/p/10295382.html
Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用相关推荐
- Android开发8:UI组件TextView,EditText,Button
版本:Android4.3 API18 学习整理:liuxinming TextView 概述 TextView直接继承了View(EditText.Button两个UI组件类的父类) TextVie ...
- Android Studio --- [学习笔记]Button、TextView、EditText
说明 源代码 为了更全面的了解RN,先熟悉一下Android开发 第1章 Android 初体验 1.1 Android开发概述 Android是Google开发的操作系统 Android开发是移动应 ...
- 简单java登录页面android_Android 之路 - RxJava2+Retrofit实现简单登陆
原标题:Android 之路 - RxJava2+Retrofit实现简单登陆 前言 使用RxJava2+Retrofit实现简单登陆. 正文1. Hello world1.1 相关版本 Androi ...
- Android学习--02(猜猜我的星座App源码+Android常用控件TextView+EditText+Button+ImangeView+DatePicker+App间通信+跳转页面)
猜猜我的星座App 1 Android常用控件 1.1 TextView控件 1.1.1 简介 1.1.2属性 1.1.3 扩展属性 1.1.4 TextView的使用方法 1.1.5总结 1.2 E ...
- 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...
- android mysql实现登录注册_android简单登陆和注册功能实现+SQLite数据库学习
android简单登陆和注册功能实现+SQLite数据库学习 发布时间:2018-07-04 17:23, 浏览次数:1027 , 标签: android SQLite 这里我只是建立了一个用简单的存 ...
- android textview点击部分,Android开发第二大控件,EditText的属性和使用方法
EditText与TextView非常相似,它甚至与TextView 共用了绝大部分XML属性和方法.EditText与TextView的最大区别在于:EditText可以接受用户输入. 一.Edit ...
- android 添加一个button并获取editText数据
最近开始接触andriod,今天把学到的东西跟大家一起探讨.老鸟请路过... 1.新建工程(如下图) 图1 2.在layout图形界面中添加一个button和editText控件 手动从左边菜单栏中选 ...
- 高仿Android QQ2012登陆界面和注册界面
最近工作比较轻松,项目不忙,所以闲着的时间去研究了自己比较感兴趣的UI界面,确实漂亮的UI能给用户带来良好的体验,在android应用中一直尤为重要,这次模仿的是QQ2012Android版的的最新登 ...
- 使用层级观察器hierarchyviewer综合各种布局实现Android QQ2012登陆界面和注册界面...
最近工作比较轻松,项目不忙,所以闲着的时间去研究了自己比较感兴趣的UI界面,确实漂亮的UI能给用户带来良好的体验,在android应用中一直尤为重要,这次模仿的是QQ2012Android版的的最新登 ...
最新文章
- 19个语法助你打牢Python基础
- 固态存储作缓存 提升性能有绝招
- c语言选择排序_C语言——选择排序
- Android开发,你应该知道的
- 目标检测——Detectron2的学习笔记
- 【翻译】CodeMix使用教程(四):调试
- Tensorflow训练简单神经网络
- Socket编程中,阻塞与非阻塞的区别
- 用Python筛选国考职位表
- java查询三级树(三级目录)
- 线性二次调节器(LQR)
- 计算机类qq网名,最帅的qq名字
- srio 门铃_如何更改SkyBell HD门铃的LED颜色
- M8系统开发手记(2)
- 电子表格的历史:除非你知道它从何而来,否则你将无法预测它将走向何方
- 大华摄像头离线故障:要拔除SD卡
- python高斯噪声怎么去除_Python图像处理之Pillow--ImageFilter介绍
- nodejs入门04__包的创建和发布
- Unity流水账14:GL、Graphics及CommandBuffer
- XXX could not be redeployed because it could not be completely removed in the un
热门文章
- python linux开发_python之Linux开发环境安装
- MyBatis集合Spring(四)之使用Spring处理事务
- Spring源码之ApplicationContext(七)获取消息资源
- 21秋期末考试工程项目管理10324k2
- Baxter实战 (一)ubuntu14.04安装ROS-Indigo
- [mstsc]解决win8 win10 平板无法被mstsc连接的问题
- flask + apidoc 生成接口文档(附加一个坑)
- cp: 略过目录XXXXXX
- 给你自己的博客加个 Markdown
- Qt: 非阻塞时间延迟;