Android studio 开发一个用户登录界面


activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <Button 10 android:id="@+id/button" 11 android:layout_width="203dp" 12 android:layout_height="46dp" 13 android:layout_marginEnd="120dp" 14 android:layout_marginRight="120dp" 15 android:layout_marginBottom="148dp" 16 android:hint="登录" 17 android:onClick="Check" 18 app:layout_constraintBottom_toBottomOf="parent" 19 app:layout_constraintEnd_toEndOf="parent" 20 app:layout_constraintHorizontal_bias="1.0" 21 app:layout_constraintStart_toStartOf="parent" /> 22 23 <EditText 24 android:id="@+id/name" 25 android:layout_width="209dp" 26 android:layout_height="50dp" 27 android:layout_marginTop="324dp" 28 android:layout_marginEnd="92dp" 29 android:layout_marginRight="92dp" 30 android:ems="10" 31 android:hint="用户名" 32 android:inputType="textPersonName" 33 app:layout_constraintEnd_toEndOf="parent" 34 app:layout_constraintTop_toTopOf="parent" /> 35 36 <EditText 37 android:id="@+id/pass" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:layout_marginTop="200dp" 41 android:layout_marginEnd="96dp" 42 android:layout_marginRight="96dp" 43 android:ems="10" 44 android:hint="密码" 45 android:inputType="textPersonName" 46 app:layout_constraintEnd_toEndOf="parent" 47 app:layout_constraintTop_toTopOf="parent" /> 48 49 50 </android.support.constraint.ConstraintLayout>

MainActivity.java
1 package com.example.myapplication;
2
3 import android.os.Bundle;
4 import android.support.v7.app.AppCompatActivity;
5 import android.view.View;
6 import android.widget.EditText;
7 import android.widget.Toast;
8
9 public class MainActivity extends AppCompatActivity {
10 EditText name; //用户名
11 EditText pass; //密码
12 @Override
13 protected void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_main);
16
17 //写代码
18 name=(EditText) findViewById(R.id.name); //获取用户名
19 pass=(EditText) findViewById(R.id.pass); //获取密码
20
21 }
22 //登录验证代码
23 public void Check(View v) {
24 String mname = "hello";
25 String mpass = "1234";
26 String user = name.getText().toString().trim();
27 String pwd = pass.getText().toString().trim();
28 if (user.equals(mname) && pwd.equals(mpass)) {
29 Toast.makeText(this, "恭喜,通过", Toast.LENGTH_SHORT).show();
30 } else {
31 Toast.makeText(this, "很遗憾,继续努力", Toast.LENGTH_SHORT).show();
32 }
33 }
34
35
36 }
来源:https://www.cnblogs.com/lisa2016/p/10920075.html