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简单应用的更多相关文章

  1. Android开发8:UI组件TextView,EditText,Button

    版本:Android4.3 API18 学习整理:liuxinming TextView 概述 TextView直接继承了View(EditText.Button两个UI组件类的父类) TextVie ...

  2. Android 动态的给Button、TextView、ImageView等控件设置了background后,再设置padding属性时该属性不起作用

    也许大家遇到这样一个问题,有时我们根据业务需要在一个ViewGroup中动态的(程序运行过程中)添加View.例如添加Button,就需要给Button添加background.padding.mar ...

  3. Android Studio登陆界面+Button不变色问题

    今日所学内容: 1.初始相对布局 2.AS登录界面 3.一个可以下载小图标的阿里的网站iconfont-阿里巴巴矢量图标库 用GitHub账号绑定就可以免费下载 4.取颜色工具ColorCop 遇到的 ...

  4. android入门系列- TextView EditText Button ImageView 的简单应用

    第一篇原创,其实自己就是一菜鸟,简单分享点基本知识吧.希望能有所帮助吧. TextView EditText Button ImageView 这几个控件可能是Android开发中最常用.最基本的几个 ...

  5. Android简单登陆页面

    布局: 线性布局+相对布局 日志打印: 利用LogCat和System.out.println打印观察. Onclick事件是采用过的第四种: 在配置文件中给Button添加点击时间 涉及知识: 通过 ...

  6. Android笔记-4-实现登陆页面并跳转和简单的注册页面

    实现登陆页面并跳转和简单的注册页面   首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px; ...

  7. 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...

  8. Android UI 设计之 TextView EditText 组件属性方法最详细解析

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...

  9. Android 有关在ListView RecycleView 中使用EditText Checkbox的坑

    这是一篇文字超多的博客,哈哈哈,废话自行过滤··· 遇到问题 在开发中我们常会在ListView , RecycleView 列表中添加EditText输入框,或者checkbox复选框.   复选框 ...

随机推荐

  1. Android 音视频开发(一) : 通过三种方式绘制图片

    版权声明:转载请说明出处:http://www.cnblogs.com/renhui/p/7456956.html 在 Android 音视频开发学习思路 里面,我们写到了,想要逐步入门音视频开发,就 ...

  2. Android 音视频开发(四):使用 Camera API 采集视频数据

    本文主要将的是:使用 Camera API 采集视频数据并保存到文件,分别使用 SurfaceView.TextureView 来预览 Camera 数据,取到 NV21 的数据回调. 注: 需要权限 ...

  3. [Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  4. [Swift]LeetCode481. 神奇字符串 | Magical String

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  5. [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  6. [Swift]LeetCode841. 钥匙和房间 | Keys and Rooms

    There are N rooms and you start in room 0.  Each room has a distinct number in 0, 1, 2, ..., N-1, an ...

  7. [Swift]LeetCode938. 二叉搜索树的范围和 | Range Sum of BST

    Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...

  8. windows下golang实现Kfaka消息发送及kafka环境搭建

    kafka环境搭建: 一.安装配置java-jdk (1)kafka需要java环境,安装java-jdk,下载地址:https://www.oracle.com/technetwork/java/j ...

  9. asp.net core 系列 4 注入服务的生存期

    一.服务的生存期 在容器中每个注册的服务,根据程序应用需求都可以选择合适的服务生存期,ASP.NET Core 服务有三种生存期配置: (1) Transient:暂时生存期,在每次请求时被创建. 这 ...

  10. 环境与工具2:建立高效的mac环境

    你的工作与生活离不开电脑,电脑是一个工具,也是一个环境.环境是不是绿水青山,是不是得心应手,这是很重要的事情.小程平时使用macbook来学习跟娱乐,最近重装了系统,很多环境与工具都需要重新组建. 那 ...