1.MainActivity

package com.example.flyyu.second2;import android.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {         @Override    protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    AlertDialog dialog;    @Override    public void onClick(View v) {             AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);        LayoutInflater inflater = MainActivity.this.getLayoutInflater();        final View view1 = inflater.inflate(R.layout.layout, null);        view1.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {                 @Override            public void onClick(View v) {                     final EditText edittxt_name = (EditText) view1.findViewById(R.id.editText);                final EditText edittxt_password = (EditText) view1.findViewById(R.id.editText2);                if (edittxt_name.getText().toString().equals("admin") && edittxt_password.getText().toString().equals("123456")) {                         dialog.cancel();                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();                } else {                         Toast.makeText(MainActivity.this, "错误的用户名或密码", Toast.LENGTH_SHORT).show();                }            }        });        view1.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {                 @Override            public void onClick(View v) {                     dialog.cancel();            }        });        dialog = builder.setCancelable(false).setView(view1).show();    }}

2.activity_main

<?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.flyyu.second2.MainActivity">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="onClick"        android:text="登录"        app:layout_constraintTop_toTopOf="parent"        android:layout_marginTop="8dp"        android:layout_marginLeft="16dp"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintBottom_toBottomOf="parent"        android:layout_marginBottom="8dp"        app:layout_constraintVertical_bias="0.498"        android:layout_marginRight="8dp"        app:layout_constraintRight_toRightOf="parent" /></android.support.constraint.ConstraintLayout>

3.layout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <ImageView            android:id="@+id/imageView"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            app:srcCompat="@mipmap/ic_launcher" />        <TextView            android:id="@+id/textView1"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:text=" 用户登录"            android:textAlignment="viewStart"            android:textColor="@android:color/holo_orange_light"            android:textSize="24sp" />    </LinearLayout>    <TextView        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text=" 请输入用户名和密码"        android:textSize="18sp" />    <TextView        android:id="@+id/textView3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="用户名:"        android:textSize="18sp" />    <EditText        android:id="@+id/editText"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="text" />    <TextView        android:id="@+id/textView4"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="密码:"        android:textSize="18sp" />    <EditText        android:id="@+id/editText2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="numberPassword" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_cancle"            android:layout_width="191dp"            android:layout_height="wrap_content"            android:text="取消" />        <Button            android:id="@+id/btn_ok"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="确定" />    </LinearLayout></LinearLayout>

更多相关文章

  1. android-RelativeLayout实现顶部、中部、底部布局
  2. Android(安卓)UI控件学习笔记(二)
  3. 个人仿制android QQ、android大作业
  4. 【Android】Android取消EditText自动获取焦点
  5. 登录案例_android
  6. Android(安卓)之 自定义控件用法介绍
  7. 登录XML
  8. Android仿真器远程登录模式(telnet)
  9. Android仿真器远程登录模式(telnet)

随机推荐

  1. 使用原生 js 完成一个购物车页面和 ES6
  2. 程序员翻车时的 30 种常见反应!
  3. MySQL命令操作
  4. 一分钟上手Docker容器
  5. Spring Cloud微服务Sentinel+Apollo限流
  6. 写了10年JAVA代码,为何还是给人一种乱糟糟
  7. Python入门课程零基础到精通——基本数学
  8. 漫画:聊一聊MVC、MVP、MVVM?
  9. 漫画:架构师是吧?什么是哈希轮?
  10. Sprint Boot如何基于Redis发布订阅实现异