簡單的本地驗證

本實驗是簡單的登錄界面—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("");

        }
    }
}

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章