第三次作业

<?xml version="1.0" encoding="UTF-8"?>

-<androidx.constraintlayout.widget.ConstraintLayout tools:context=".MainActivity" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">

<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" android:inputType="textPassword" android:hint="请输入密码" android:ems="10" android:layout_marginTop="272dp" android:layout_marginLeft="96dp" android:layout_marginStart="96dp" android:id="@+id/editText2"/>

<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" app:layout_constraintStart_toStartOf="@+id/editText2" android:inputType="textPersonName" android:hint="请输入账号" android:ems="10" android:id="@+id/editText" app:layout_constraintBottom_toTopOf="@+id/editText2" android:layout_marginBottom="64dp"/>

<Button android:layout_height="wrap_content" android:layout_width="wrap_content" app:layout_constraintStart_toStartOf="@+id/editText2" android:layout_marginTop="65dp" android:layout_marginLeft="60dp" android:layout_marginStart="60dp" android:id="@+id/button" app:layout_constraintTop_toBottomOf="@+id/editText2" android:text="登陆"/>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText)
                findViewById(R.id.editText);
        password=(EditText)
                findViewById(R.id.editText2);
        btn = (Button)
                findViewById(R.id.button);
        btn.setOnClickListener(new
                                       View.OnClickListener() {
                                           public void onClick(View view) {
                                               String strUsername =
                                                       username.getText().toString();
                                               String strPassword =
                                                       password.getText().toString();
                                               //判断正误
                                               if
                                               (strUsername.equals("hello") && strPassword.equals("567890")) {
                                                   Toast.makeText(MainActivity.this, "login successfully", Toast.LENGTH_SHORT).show();
                                               } else {
                                                   Toast.makeText(MainActivity.this, "login failure", Toast.LENGTH_SHORT).show();
                                               }
                                               btn.setOnClickListener(new View.OnClickListener() {
                                                   @Override
                                                   public void
                                                   onClick(View view) {
                                                       finish();
                                                   }
                                               });
                                           }
                                       });
    }
}

posted @ 2019-09-17 15:20  blinkQ  阅读(135)  评论(0)    收藏  举报