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]LeetCode136. 只出现一次的数字 | Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- 超全MyBatis动态SQL详解!( 看完SQL爽多了)
MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...
- javascript 使用小技巧总结
按位取反 ~a 即:返回 -(a+1),会去掉小数点. let a = 3.14; let b = ~a; //b = -(3.14+1) 取整 为-4: let c = ~b; //c = -(-4 ...
- iOS学习——页面的传值方式
一.简述 在iOS开发过程中,页面跳转时在页面之间进行数据传递是很常见的事情,我们称这个过程为页面传值.页面跳转过程中,从主页面跳转到子页面的数据传递称之为正向传值:反之,从子页面返回主页面时的数据传 ...
- Map 转换成byte[] 数组
把Map转换成byte数组,使用 ByteArrayOutputStream和ObjectOutputStream Map<String,String> map = new HashMap ...
- Git的使用--如何将本地项目上传到Github(三种简单、方便的方法)
一.第一种方法: 1.首先你需要一个github账号,所以还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路(傻瓜式安装) ...
- 有了这 4 大特性,CDN 好用到飞起
随着 CDN 市场的快速发展和网络新技术的不断涌现,目前的 CDN 已不仅仅是当初简单的内容分发,同时也是新特性研发.新技术推广及实践的平台.这些新技术.新特性,或者能够保障 CDN 安全性,或是提升 ...
- java代码之美(9)---guava之Lists、Maps
guava之Lists.Maps 谷歌提供了guava包里面有很多的工具类,Lists和Maps集合工具,集合操作做了些优化提升. 1.概述 1.静态工厂方法 (1)Guava提供了能够推断范型的静态 ...
- 『最小表示法 Necklace』
最小表示法 这是一个简单的字符串算法,其解决的问题如下: 给定一个字符串\(S\),长度为\(n\),如果把它的最后一个字符不断放到最前面,会得到\(n\)个不同的字符串,那么我们称这\(n\)个字符 ...
- 『字典树 trie』
字典树 (trie) 字典树,又名\(trie\)树,是一种用于实现字符串快速检索的树形数据结构.核心思想为利用若干字符串的公共前缀来节约储存空间以及实现快速检索. \(trie\)树可以在\(O(( ...