Android Studio对话框登录。。。

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>

你可能感兴趣的:(android,studio,android,app)