<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="显示密码"></CheckBox>
设置显示密码框,采用的是<checkbox>(未学)
android:hint="请输入密码"
android:password="true"
使密码输入后变成*号,hint输入是根据提示进行输入,当输入数据时提示会消失,再根据password属性,使输入的密码变为*号
布局文件 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#E0F7FA">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎使用登陆界面"
android:layout_gravity="center"
android:gravity="center"
android:textSize="24sp"
android:textColor="@color/black"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:background="#E3F2FD"
android:orientation="vertical">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@mipmap/bg1"
android:layout_marginLeft="135dp"
>
</ImageView>
<ImageView
android:layout_width="60dp"
android:layout_height="50dp"
android:src="@mipmap/yonghu"
android:layout_marginLeft="135dp"
android:layout_margin="20dp"
>
</ImageView>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_marginRight="40dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="-60dp"></EditText>
<ImageView
android:layout_width="60dp"
android:layout_height="50dp"
android:src="@mipmap/dongjie"
android:layout_marginLeft="135dp"
android:layout_margin="20dp"
>
</ImageView>
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:password="true"
android:layout_marginRight="40dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="-60dp"></EditText>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="显示密码"></CheckBox>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码 ?"
android:layout_marginLeft="270dp"
android:textSize="16sp"
android:layout_marginTop="-32dp"></TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#FFFFF5EE"
android:orientation="horizontal">
<Button
android:id="@+id/bd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="70dp"
android:text="登录"
android:textColor="@color/white"
android:background="#43A047"
></Button>
<Button
android:id="@+id/bd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="70dp"
android:text="注册"
android:textColor="@color/white"
android:background="#E53935"
></Button>
</RelativeLayout>
</LinearLayout>a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/zwh"
android:scaleType="centerCrop"
></ImageView>
</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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#E0F7FA">
<Button
android:id="@id/bd11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BackSpace"
android:textSize="18sp"
android:textColor="#ff0000"
></Button>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欢迎使用注册界面"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:textSize="24sp"
android:textColor="@color/black"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:background="#E3F2FD"
android:orientation="vertical">
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@mipmap/bg1"
android:layout_marginLeft="135dp"
>
</ImageView>
<ImageView
android:layout_width="60dp"
android:layout_height="50dp"
android:src="@mipmap/yonghu"
android:layout_marginLeft="135dp"
android:layout_margin="20dp"
>
</ImageView>
<EditText
android:id="@+id/name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的用户名"
android:layout_marginRight="40dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="-60dp"></EditText>
<ImageView
android:layout_width="60dp"
android:layout_height="50dp"
android:src="@mipmap/dongjie"
android:layout_marginLeft="135dp"
android:layout_margin="20dp"
>
</ImageView>
<EditText
android:id="@+id/pwd1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入注册密码"
android:password="true"
android:layout_marginRight="40dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="-60dp"></EditText>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="70dp"
android:layout_marginBottom="-30dp"
android:text="显示密码"></CheckBox>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#FFFFF5EE"
android:orientation="horizontal">
<Button
android:id="@+id/bd12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:text="注册"
android:textColor="@color/white"
android:background="#43A047"
android:textSize="30sp"
></Button
>
</RelativeLayout>
</LinearLayout>
登录,注册按钮的设置,界面的布局。
存放转场图片,登陆成功时显示
跟登陆界面代码基本相同,甚至是直接复制,多了个退出界面
static ArrayList<String> arrayuser=new ArrayList<String>();
static ArrayList<String> arraypwd=new ArrayList<String>();
public static void adduser(String s1) {
arrayuser.add(s1);
}
public static void addpwd(String s2) {
arraypwd.add(s2);
}
创建两个静态集合对象以及方法,用于储存注册过的账户跟密码
创建两个对象,一个用于添加密码,一个用于添加账户
if (arrayuser.contains(Tem_id) & arraypwd.contains(Tem_passw)) {
Toast.makeText(TestActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(TestActivity.this, MyActivity.class);
//启动Intent
startActivity(intent);
}
else {
if (Tem_id.isEmpty() | Tem_passw.isEmpty()) {
Toast.makeText(TestActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(TestActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
用来判断登录问题,根据不同的情况虚拟机灰报不同的语句
Toast.makeText(TestActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
当鼠标点击事件启动,这行文字将会在虚拟机上显示
checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
psd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
psd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
如果被点击的话,就显示密码 ,显示密码功能的设置
Intent intent = new Intent(TestActivity.this, MyActivity.class);
//启动Intent
startActivity(intent);
转场的设置,最后一个变量表示的是“你想要去哪” 。
if(TestActivity.arrayuser.contains(Tem_id1))
{
Toast.makeText(RegisterActivity.this, "您已注册过此账户,不能重复注册", Toast.LENGTH_SHORT).show();
}
else {
if (Tem_id1.isEmpty() || Tem_passw1.isEmpty()) {
Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
TestActivity.adduser(Tem_id1);
TestActivity.addpwd(Tem_passw1);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}
}
这部分是注册功能的实现,如果用户名已存在的话,将提示已注册过,并且不会添加到集合中去,如果用户名不存在,则将会把用户名跟密码添加到集合中,表示注册成功
Activity文件
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture);
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
private CheckBox checkbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button bd11=findViewById(R.id.bd11);
Button bd12=findViewById(R.id.bd12);
EditText user1=(EditText) findViewById(R.id.name1);
EditText psd1=(EditText) findViewById(R.id.pwd1);
checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
psd1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
psd1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
bd11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RegisterActivity.this, TestActivity.class);
startActivity(intent);
}
});
bd12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Tem_id1=user1.getText().toString();
String Tem_passw1=psd1.getText().toString();
if(TestActivity.arrayuser.contains(Tem_id1))
{
Toast.makeText(RegisterActivity.this, "您已注册过此账户,不能重复注册", Toast.LENGTH_SHORT).show();
}
else {
if (Tem_id1.isEmpty() || Tem_passw1.isEmpty()) {
Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
TestActivity.adduser(Tem_id1);
TestActivity.addpwd(Tem_passw1);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class TestActivity extends AppCompatActivity {
private CheckBox checkbox;
static ArrayList<String> arrayuser=new ArrayList<String>();
static ArrayList<String> arraypwd=new ArrayList<String>();
public static void adduser(String s1) {
arrayuser.add(s1);
}
public static void addpwd(String s2) {
arraypwd.add(s2);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
Button bd1=findViewById(R.id.bd1);
Button bd2=findViewById(R.id.bd2);
EditText user=(EditText) findViewById(R.id.name);
EditText psd=(EditText) findViewById(R.id.pwd);
String str_id="admin";
TestActivity.adduser(str_id);
String str_passw="123456";
TestActivity.addpwd(str_passw);
checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
psd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
psd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
bd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Tem_id=user.getText().toString();
String Tem_passw=psd.getText().toString();
if (arrayuser.contains(Tem_id) & arraypwd.contains(Tem_passw)) {
Toast.makeText(TestActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(TestActivity.this, MyActivity.class);
//启动Intent
startActivity(intent);
}
else {
if (Tem_id.isEmpty() | Tem_passw.isEmpty()) {
Toast.makeText(TestActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(TestActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
}
});
bd2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(TestActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}