简单的本地验证

来源:互联网 发布:直销双轨奖金制度算法 编辑:程序博客网 时间:2024/05/17 02:00

本实验是简单的登录界面—EditText编辑框的应用,在该界面中,若输入正确的用户名(假设为e1001)和密码(假设为1234567),单击“确定”按钮,将出现一个Toast提示“恭喜您登录成功!”;否则将提示“请输入正确的用户名或密码!”。单击“清空”按钮,则会清空所填写的姓名和密码内容。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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"    tools:context="com.example.hades.homework5.MainActivity">    <LinearLayout        android:layout_width="374dp"        android:layout_height="497dp"        android:orientation="vertical"        tools:layout_editor_absoluteY="6dp"        tools:layout_editor_absoluteX="8dp">        <LinearLayout            android:layout_width="374dp"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="0.77"                android:text="用户名"                android:textSize="20dp" />            <EditText                android:id="@+id/username"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPersonName"                android:text="e10001" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            tools:layout_editor_absoluteX="8dp"            tools:layout_editor_absoluteY="8dp">            <TextView                android:id="@+id/textView2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="密码"                android:textSize="20dp" />            <EditText                android:id="@+id/password"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPassword"                android:text="1234567" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <Button                android:id="@+id/login"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="登录" />            <Button                android:id="@+id/clear"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="清除" />        </LinearLayout>    </LinearLayout></android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.hades.homework5;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    Button login, clear;    TextView username, password;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        login = (Button) findViewById(R.id.login);        clear = (Button) findViewById(R.id.clear);        username = (TextView) findViewById(R.id.username);        password = (TextView) findViewById(R.id.password);        login.setOnClickListener(new click1());        clear.setOnClickListener(new click2());    }    private class click1 implements View.OnClickListener {        @Override        public void onClick(View v) {            if (username.toString().isEmpty() || password.toString().isEmpty()) {                Toast.makeText(getApplicationContext(), "请填写完整",                        Toast.LENGTH_SHORT).show();            } else {                if (username.getText().toString().equals("e10001") && password.getText().toString().equals("1234567")) {                    Toast.makeText(getApplicationContext(), "登录成功",                            Toast.LENGTH_SHORT).show();                } else {                    Toast.makeText(getApplicationContext(), "用户名或密码错误",                            Toast.LENGTH_SHORT).show();                }            }        }    }    private class click2 implements View.OnClickListener {        @Override        public void onClick(View v) {            username.setText("");            password.setText("");        }    }}

这里写图片描述

这里写图片描述