Android Studio实现简单的登陆界面

简单的小列子,要求用户名密码不能为空,这里默认了用户名为123456,密码为666666,用户点击登录后,若用户名密码全正确,显示“登陆成功”,用户名或密码未填写则显示“用户名或密码不能为空”,若用户名或密码错误则显示“用户名或密码错误”。界面效果图

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp"
        android:paddingTop="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="286dp"
            android:layout_weight="0"
            app:srcCompat="@drawable/QQ" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal"
            android:padding="20dp">

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="用户名" />

            <EditText
                android:id="@+id/edit1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName" />
        </LinearLayout>

        <LinearLayout

            android:layout_width="372dp"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal"
            android:padding="20dp"
            android:paddingTop="100dp">

            <TextView
                android:id="@+id/text2"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="密码" />

            <EditText
                android:id="@+id/edit2"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                android:password="true" />

        </LinearLayout>

        <Button
            android:id="@+id/button1"
            android:layout_width="372dp"
            android:layout_height="wrap_content"
            android:text="登录" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.Console;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 EditText edt1;
 EditText edt2;
 Button btn;
 Toast mtoast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt1=(EditText)findViewById(R.id.edit1);
        edt2=(EditText)findViewById(R.id.edit2);
        btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
       if (edt1.getText().toString().equals("")||edt2.getText().toString().equals("")) {
           showToast("用户名或密码不能为空!");
       }else if(edt1.getText().toString().equals("123456")&&edt2.getText().toString().equals("666666")){
           showToast("登录成功!!!");
       }else{
          showToast("用户名或密码错误!");
       }
        }
        private void showToast(String msg){
        mtoast=Toast.makeText(this,msg,Toast.LENGTH_SHORT);
        mtoast.show();
        }
    }




  • 20
    点赞
  • 208
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值