其他分享
首页 > 其他分享> > android --一个简单的登录界面

android --一个简单的登录界面

作者:互联网

MainActivity.java

package com.example.empty_project;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      //实现背景图全面屏 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }
      //获得控件的引用 Button button = (Button)findViewById(R.id.button); EditText username = (EditText)findViewById(R.id.username) ; EditText password = (EditText)findViewById(R.id.password) ;
      //button添加监听,OnClickListener是View类下的内部接口 button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){
          //通过getText()获得EditText的值,使用equals比较,因为equals是内容比较而==是地址比较 if(username.getText().toString().equals("admin") && password.getText().toString().equals("123456")){
          //Toast.makeText()弹框提示, 第一个参数为本类,第二个参数是内容,第三个参数是摊开停留时间 Toast.makeText(MainActivity.this,"您已成功登录",Toast.LENGTH_LONG).show(); }else{ Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG).show(); } } }); } }
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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/background"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:layout_editor_absoluteX="-115dp"
        tools:layout_editor_absoluteY="-16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFE4C4"
            android:orientation="vertical"
            android:paddingLeft="5dp"
            android:paddingRight="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="账    号:" />

                <EditText
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="输入账号"
                    android:maxLength="11" />

            </LinearLayout>


            <!--密码-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="密    码:" />

                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="请输入密码"
                    android:inputType="textPassword" />
            </LinearLayout>

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="登录"
                android:textSize="20sp"
                 />

        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

标签:Toast,界面,--,EditText,button,equals,import,android
来源: https://www.cnblogs.com/CloudComputing-binbin/p/15627205.html