首页 技术 正文
技术 2022年11月14日
0 收藏 898 点赞 2,540 浏览 6057 个字

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();
}
});
}
}

效果图::

Android 简单登陆 涉及 Button CheckBox TextView EditText简单应用

代码地址:https://github.com/1165863642/LoginDemo

涉及到的一些知识点 不懂的可以咨询我 扣:1165863642  共同学习

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849