• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

注册和登录界面的实现

武飞扬头像
m0_63911789
帮助1

<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属性,使输入的密码变为*号

布局文件 :

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:orientation="vertical"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="match_parent">
  6.  
     
  7.  
    <LinearLayout
  8.  
    android:layout_width="match_parent"
  9.  
    android:layout_height="0dp"
  10.  
    android:layout_weight="1"
  11.  
    android:background="#E0F7FA">
  12.  
     
  13.  
    <TextView
  14.  
    android:layout_width="match_parent"
  15.  
    android:layout_height="wrap_content"
  16.  
    android:text="欢迎使用登陆界面"
  17.  
    android:layout_gravity="center"
  18.  
    android:gravity="center"
  19.  
    android:textSize="24sp"
  20.  
    android:textColor="@color/black"></TextView>
  21.  
     
  22.  
     
  23.  
     
  24.  
    </LinearLayout>
  25.  
     
  26.  
    <LinearLayout
  27.  
    android:layout_width="match_parent"
  28.  
    android:layout_height="0dp"
  29.  
    android:layout_weight="6"
  30.  
    android:background="#E3F2FD"
  31.  
    android:orientation="vertical">
  32.  
     
  33.  
    <ImageView
  34.  
    android:layout_width="120dp"
  35.  
    android:layout_height="100dp"
  36.  
    android:src="@mipmap/bg1"
  37.  
    android:layout_marginLeft="135dp"
  38.  
    >
  39.  
    </ImageView>
  40.  
     
  41.  
    <ImageView
  42.  
    android:layout_width="60dp"
  43.  
    android:layout_height="50dp"
  44.  
    android:src="@mipmap/yonghu"
  45.  
    android:layout_marginLeft="135dp"
  46.  
    android:layout_margin="20dp"
  47.  
     
  48.  
    >
  49.  
    </ImageView>
  50.  
    <EditText
  51.  
    android:id="@ id/name"
  52.  
    android:layout_width="match_parent"
  53.  
    android:layout_height="wrap_content"
  54.  
    android:hint="请输入用户名"
  55.  
    android:layout_marginRight="40dp"
  56.  
    android:layout_marginLeft="80dp"
  57.  
    android:layout_marginTop="-60dp"></EditText>
  58.  
     
  59.  
    <ImageView
  60.  
    android:layout_width="60dp"
  61.  
    android:layout_height="50dp"
  62.  
    android:src="@mipmap/dongjie"
  63.  
    android:layout_marginLeft="135dp"
  64.  
    android:layout_margin="20dp"
  65.  
    >
  66.  
     
  67.  
    </ImageView>
  68.  
     
  69.  
    <EditText
  70.  
    android:id="@ id/pwd"
  71.  
    android:layout_width="match_parent"
  72.  
    android:layout_height="wrap_content"
  73.  
    android:hint="请输入密码"
  74.  
    android:password="true"
  75.  
    android:layout_marginRight="40dp"
  76.  
    android:layout_marginLeft="80dp"
  77.  
    android:layout_marginTop="-60dp"></EditText>
  78.  
     
  79.  
    <CheckBox
  80.  
    android:id="@ id/checkbox"
  81.  
    android:layout_width="wrap_content"
  82.  
    android:layout_height="wrap_content"
  83.  
    android:layout_marginLeft="70dp"
  84.  
    android:text="显示密码"></CheckBox>
  85.  
     
  86.  
    <TextView
  87.  
    android:layout_width="wrap_content"
  88.  
    android:layout_height="wrap_content"
  89.  
    android:text="忘记密码 ?"
  90.  
    android:layout_marginLeft="270dp"
  91.  
    android:textSize="16sp"
  92.  
    android:layout_marginTop="-32dp"></TextView>
  93.  
     
  94.  
     
  95.  
     
  96.  
     
  97.  
     
  98.  
    </LinearLayout>
  99.  
     
  100.  
     
  101.  
     
  102.  
    <RelativeLayout
  103.  
    android:layout_width="match_parent"
  104.  
    android:layout_height="0dp"
  105.  
    android:layout_weight="4"
  106.  
    android:background="#FFFFF5EE"
  107.  
    android:orientation="horizontal">
  108.  
     
  109.  
    <Button
  110.  
    android:id="@ id/bd1"
  111.  
    android:layout_width="wrap_content"
  112.  
    android:layout_height="wrap_content"
  113.  
    android:gravity="center"
  114.  
    android:layout_marginLeft="70dp"
  115.  
    android:text="登录"
  116.  
    android:textColor="@color/white"
  117.  
    android:background="#43A047"
  118.  
    ></Button>
  119.  
     
  120.  
    <Button
  121.  
    android:id="@ id/bd2"
  122.  
    android:layout_width="wrap_content"
  123.  
    android:layout_height="wrap_content"
  124.  
    android:layout_alignParentRight="true"
  125.  
    android:layout_marginRight="70dp"
  126.  
    android:text="注册"
  127.  
    android:textColor="@color/white"
  128.  
    android:background="#E53935"
  129.  
    ></Button>
  130.  
    </RelativeLayout>
  131.  
     
  132.  
    </LinearLayout>a
学新通
  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:orientation="vertical"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="match_parent">
  6.  
     
  7.  
    <ImageView
  8.  
    android:layout_width="match_parent"
  9.  
    android:layout_height="match_parent"
  10.  
    android:src="@drawable/zwh"
  11.  
    android:scaleType="centerCrop"
  12.  
    ></ImageView>
  13.  
     
  14.  
     
  15.  
     
  16.  
    </LinearLayout>
学新通
  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:app="http://schemas.android.com/apk/res-auto"
  4.  
    xmlns:tools="http://schemas.android.com/tools"
  5.  
    android:layout_width="match_parent"
  6.  
    android:orientation="vertical"
  7.  
    android:layout_height="match_parent"
  8.  
    tools:context=".RegisterActivity">
  9.  
     
  10.  
    <LinearLayout
  11.  
    android:layout_width="match_parent"
  12.  
    android:layout_height="0dp"
  13.  
    android:layout_weight="1"
  14.  
    android:background="#E0F7FA">
  15.  
    <Button
  16.  
    android:id="@id/bd11"
  17.  
    android:layout_width="wrap_content"
  18.  
    android:layout_height="wrap_content"
  19.  
    android:text="BackSpace"
  20.  
    android:textSize="18sp"
  21.  
    android:textColor="#ff0000"
  22.  
    ></Button>
  23.  
     
  24.  
    <TextView
  25.  
    android:layout_width="match_parent"
  26.  
    android:layout_height="wrap_content"
  27.  
    android:text="欢迎使用注册界面"
  28.  
    android:layout_gravity="center"
  29.  
    android:layout_marginTop="15dp"
  30.  
    android:textSize="24sp"
  31.  
    android:textColor="@color/black"></TextView>
  32.  
     
  33.  
     
  34.  
     
  35.  
     
  36.  
     
  37.  
     
  38.  
    </LinearLayout>
  39.  
     
  40.  
    <LinearLayout
  41.  
    android:layout_width="match_parent"
  42.  
    android:layout_height="0dp"
  43.  
    android:layout_weight="6"
  44.  
    android:background="#E3F2FD"
  45.  
    android:orientation="vertical">
  46.  
     
  47.  
    <ImageView
  48.  
    android:layout_width="120dp"
  49.  
    android:layout_height="100dp"
  50.  
    android:src="@mipmap/bg1"
  51.  
    android:layout_marginLeft="135dp"
  52.  
    >
  53.  
    </ImageView>
  54.  
     
  55.  
    <ImageView
  56.  
    android:layout_width="60dp"
  57.  
    android:layout_height="50dp"
  58.  
    android:src="@mipmap/yonghu"
  59.  
    android:layout_marginLeft="135dp"
  60.  
    android:layout_margin="20dp"
  61.  
     
  62.  
    >
  63.  
    </ImageView>
  64.  
    <EditText
  65.  
    android:id="@ id/name1"
  66.  
    android:layout_width="match_parent"
  67.  
    android:layout_height="wrap_content"
  68.  
    android:hint="请输入您的用户名"
  69.  
    android:layout_marginRight="40dp"
  70.  
    android:layout_marginLeft="80dp"
  71.  
    android:layout_marginTop="-60dp"></EditText>
  72.  
     
  73.  
    <ImageView
  74.  
    android:layout_width="60dp"
  75.  
    android:layout_height="50dp"
  76.  
    android:src="@mipmap/dongjie"
  77.  
    android:layout_marginLeft="135dp"
  78.  
    android:layout_margin="20dp"
  79.  
    >
  80.  
     
  81.  
    </ImageView>
  82.  
     
  83.  
    <EditText
  84.  
    android:id="@ id/pwd1"
  85.  
    android:layout_width="match_parent"
  86.  
    android:layout_height="wrap_content"
  87.  
    android:hint="请输入注册密码"
  88.  
    android:password="true"
  89.  
    android:layout_marginRight="40dp"
  90.  
    android:layout_marginLeft="80dp"
  91.  
    android:layout_marginTop="-60dp"></EditText>
  92.  
     
  93.  
     
  94.  
    <CheckBox
  95.  
    android:id="@ id/checkbox"
  96.  
    android:layout_width="match_parent"
  97.  
    android:layout_height="match_parent"
  98.  
    android:layout_marginLeft="70dp"
  99.  
    android:layout_marginBottom="-30dp"
  100.  
    android:text="显示密码"></CheckBox>
  101.  
     
  102.  
     
  103.  
     
  104.  
     
  105.  
     
  106.  
     
  107.  
    </LinearLayout>
  108.  
     
  109.  
     
  110.  
     
  111.  
    <RelativeLayout
  112.  
    android:layout_width="match_parent"
  113.  
    android:layout_height="0dp"
  114.  
    android:layout_weight="4"
  115.  
    android:background="#FFFFF5EE"
  116.  
    android:orientation="horizontal">
  117.  
     
  118.  
    <Button
  119.  
    android:id="@ id/bd12"
  120.  
    android:layout_width="wrap_content"
  121.  
    android:layout_height="wrap_content"
  122.  
    android:gravity="center"
  123.  
    android:layout_centerHorizontal="true"
  124.  
    android:text="注册"
  125.  
    android:textColor="@color/white"
  126.  
    android:background="#43A047"
  127.  
    android:textSize="30sp"
  128.  
    ></Button
  129.  
    >
  130.  
     
  131.  
    </RelativeLayout>
  132.  
     
  133.  
    </LinearLayout>
学新通

登录,注册按钮的设置,界面的布局。

存放转场图片,登陆成功时显示

跟登陆界面代码基本相同,甚至是直接复制,多了个退出界面

  1.  
    static ArrayList<String> arrayuser=new ArrayList<String>();
  2.  
    static ArrayList<String> arraypwd=new ArrayList<String>();
  3.  
    public static void adduser(String s1) {
  4.  
    arrayuser.add(s1);
  5.  
    }
  6.  
    public static void addpwd(String s2) {
  7.  
    arraypwd.add(s2);
  8.  
    }

创建两个静态集合对象以及方法,用于储存注册过的账户跟密码

创建两个对象,一个用于添加密码,一个用于添加账户

  1.  
    if (arrayuser.contains(Tem_id) & arraypwd.contains(Tem_passw)) {
  2.  
    Toast.makeText(TestActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
  3.  
    Intent intent = new Intent(TestActivity.this, MyActivity.class);
  4.  
     
  5.  
    //启动Intent
  6.  
    startActivity(intent);
  7.  
     
  8.  
    }
  9.  
    else {
  10.  
    if (Tem_id.isEmpty() | Tem_passw.isEmpty()) {
  11.  
    Toast.makeText(TestActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
  12.  
    } else {
  13.  
    Toast.makeText(TestActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
  14.  
    }
  15.  
    }
学新通

 用来判断登录问题,根据不同的情况虚拟机灰报不同的语句

Toast.makeText(TestActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();

当鼠标点击事件启动,这行文字将会在虚拟机上显示

  1.  
  2.  
    checkbox = (CheckBox) findViewById(R.id.checkbox);
  3.  
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  4.  
     
  5.  
     
  6.  
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  7.  
     
  8.  
    if (isChecked) {
  9.  
    psd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
  10.  
    } else {
  11.  
    psd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  12.  
    }
  13.  
     
  14.  
    }
  15.  
    });
  16.  
     
  17.  
学新通

如果被点击的话,就显示密码 ,显示密码功能的设置

  1.  
    Intent intent = new Intent(TestActivity.this, MyActivity.class);
  2.  
     
  3.  
    //启动Intent
  4.  
    startActivity(intent);

转场的设置,最后一个变量表示的是“你想要去哪” 。

  1.  
    if(TestActivity.arrayuser.contains(Tem_id1))
  2.  
    {
  3.  
    Toast.makeText(RegisterActivity.this, "您已注册过此账户,不能重复注册", Toast.LENGTH_SHORT).show();
  4.  
    }
  5.  
    else {
  6.  
    if (Tem_id1.isEmpty() || Tem_passw1.isEmpty()) {
  7.  
    Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
  8.  
    } else {
  9.  
    TestActivity.adduser(Tem_id1);
  10.  
    TestActivity.addpwd(Tem_passw1);
  11.  
    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
  12.  
    }
  13.  
    }

 这部分是注册功能的实现,如果用户名已存在的话,将提示已注册过,并且不会添加到集合中去,如果用户名不存在,则将会把用户名跟密码添加到集合中,表示注册成功

Activity文件

  1.  
    package com.example.myapplication;
  2.  
     
  3.  
    import androidx.appcompat.app.AppCompatActivity;
  4.  
    import android.os.Bundle;
  5.  
     
  6.  
    public class MyActivity extends AppCompatActivity {
  7.  
     
  8.  
    @Override
  9.  
    protected void onCreate(Bundle savedInstanceState) {
  10.  
    super.onCreate(savedInstanceState);
  11.  
    setContentView(R.layout.picture);
  12.  
    }
  13.  
    }
  1.  
    package com.example.myapplication;
  2.  
     
  3.  
    import androidx.appcompat.app.AppCompatActivity;
  4.  
     
  5.  
    import android.content.Intent;
  6.  
    import android.os.Bundle;
  7.  
    import android.text.InputType;
  8.  
    import android.view.View;
  9.  
    import android.widget.Button;
  10.  
    import android.widget.CheckBox;
  11.  
    import android.widget.CompoundButton;
  12.  
    import android.widget.EditText;
  13.  
    import android.widget.Toast;
  14.  
     
  15.  
    public class RegisterActivity extends AppCompatActivity {
  16.  
    private CheckBox checkbox;
  17.  
     
  18.  
    @Override
  19.  
    protected void onCreate(Bundle savedInstanceState) {
  20.  
    super.onCreate(savedInstanceState);
  21.  
    setContentView(R.layout.activity_register);
  22.  
    Button bd11=findViewById(R.id.bd11);
  23.  
    Button bd12=findViewById(R.id.bd12);
  24.  
     
  25.  
     
  26.  
     
  27.  
    EditText user1=(EditText) findViewById(R.id.name1);
  28.  
    EditText psd1=(EditText) findViewById(R.id.pwd1);
  29.  
    checkbox = (CheckBox) findViewById(R.id.checkbox);
  30.  
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  31.  
     
  32.  
     
  33.  
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  34.  
     
  35.  
    if (isChecked) {
  36.  
    psd1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
  37.  
    } else {
  38.  
    psd1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  39.  
    }
  40.  
     
  41.  
    }
  42.  
    });
  43.  
    bd11.setOnClickListener(new View.OnClickListener() {
  44.  
    @Override
  45.  
    public void onClick(View view) {
  46.  
    Intent intent = new Intent(RegisterActivity.this, TestActivity.class);
  47.  
    startActivity(intent);
  48.  
    }
  49.  
    });
  50.  
     
  51.  
    bd12.setOnClickListener(new View.OnClickListener() {
  52.  
    @Override
  53.  
    public void onClick(View view) {
  54.  
    String Tem_id1=user1.getText().toString();
  55.  
    String Tem_passw1=psd1.getText().toString();
  56.  
    if(TestActivity.arrayuser.contains(Tem_id1))
  57.  
    {
  58.  
    Toast.makeText(RegisterActivity.this, "您已注册过此账户,不能重复注册", Toast.LENGTH_SHORT).show();
  59.  
    }
  60.  
    else {
  61.  
    if (Tem_id1.isEmpty() || Tem_passw1.isEmpty()) {
  62.  
    Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
  63.  
    } else {
  64.  
    TestActivity.adduser(Tem_id1);
  65.  
    TestActivity.addpwd(Tem_passw1);
  66.  
    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
  67.  
    }
  68.  
    }
  69.  
     
  70.  
    }
  71.  
    });
  72.  
     
  73.  
    }
  74.  
    }
学新通
  1.  
    package com.example.myapplication;
  2.  
     
  3.  
    import androidx.appcompat.app.AppCompatActivity;
  4.  
    import android.os.Bundle;
  5.  
    import android.content.Intent;
  6.  
    import android.net.Uri;
  7.  
    import android.text.InputType;
  8.  
    import android.view.View;
  9.  
    import android.widget.Button;
  10.  
    import android.widget.CheckBox;
  11.  
    import android.widget.CompoundButton;
  12.  
    import android.widget.EditText;
  13.  
    import android.widget.Toast;
  14.  
     
  15.  
    import java.util.ArrayList;
  16.  
     
  17.  
    public class TestActivity extends AppCompatActivity {
  18.  
    private CheckBox checkbox;
  19.  
     
  20.  
    static ArrayList<String> arrayuser=new ArrayList<String>();
  21.  
    static ArrayList<String> arraypwd=new ArrayList<String>();
  22.  
    public static void adduser(String s1) {
  23.  
    arrayuser.add(s1);
  24.  
    }
  25.  
    public static void addpwd(String s2) {
  26.  
    arraypwd.add(s2);
  27.  
    }
  28.  
    @Override
  29.  
    protected void onCreate(Bundle savedInstanceState) {
  30.  
    super.onCreate(savedInstanceState);
  31.  
    setContentView(R.layout.test_layout);
  32.  
     
  33.  
    Button bd1=findViewById(R.id.bd1);
  34.  
    Button bd2=findViewById(R.id.bd2);
  35.  
    EditText user=(EditText) findViewById(R.id.name);
  36.  
    EditText psd=(EditText) findViewById(R.id.pwd);
  37.  
    String str_id="admin";
  38.  
    TestActivity.adduser(str_id);
  39.  
    String str_passw="123456";
  40.  
    TestActivity.addpwd(str_passw);
  41.  
    checkbox = (CheckBox) findViewById(R.id.checkbox);
  42.  
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  43.  
     
  44.  
     
  45.  
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  46.  
     
  47.  
    if (isChecked) {
  48.  
    psd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
  49.  
    } else {
  50.  
    psd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  51.  
    }
  52.  
     
  53.  
    }
  54.  
    });
  55.  
     
  56.  
     
  57.  
    bd1.setOnClickListener(new View.OnClickListener() {
  58.  
    @Override
  59.  
    public void onClick(View view) {
  60.  
    String Tem_id=user.getText().toString();
  61.  
    String Tem_passw=psd.getText().toString();
  62.  
     
  63.  
    if (arrayuser.contains(Tem_id) & arraypwd.contains(Tem_passw)) {
  64.  
    Toast.makeText(TestActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
  65.  
    Intent intent = new Intent(TestActivity.this, MyActivity.class);
  66.  
     
  67.  
    //启动Intent
  68.  
    startActivity(intent);
  69.  
     
  70.  
    }
  71.  
    else {
  72.  
    if (Tem_id.isEmpty() | Tem_passw.isEmpty()) {
  73.  
    Toast.makeText(TestActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
  74.  
    } else {
  75.  
    Toast.makeText(TestActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
  76.  
    }
  77.  
    }
  78.  
     
  79.  
     
  80.  
     
  81.  
     
  82.  
     
  83.  
    }
  84.  
     
  85.  
     
  86.  
    });
  87.  
     
  88.  
    bd2.setOnClickListener(new View.OnClickListener() {
  89.  
    @Override
  90.  
    public void onClick(View view) {
  91.  
    Intent intent = new Intent(TestActivity.this, RegisterActivity.class);
  92.  
    startActivity(intent);
  93.  
    }
  94.  
    });
  95.  
    }
  96.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfahia
系列文章
更多 icon
同类精品
更多 icon
继续加载