<Android's research project three training> User Login dialog box design

Project Screenshot

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout 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     android:background="?attr/colorButtonNormal"
  8     tools:context=".LoginActivity">
  9 
 10     <LinearLayout
 11         android:layout_width="match_parent"
 12         android:layout_height="wrap_content"
 13         android:layout_gravity="center"
 14         android:layout_margin="30dp"
 15         android:background="@android:color/black"
 16         android:orientation="vertical">
 17 
 18         <LinearLayout
 19             android:layout_width="match_parent"
 20             android:layout_height="match_parent"
 21             android:orientation="vertical">
 22 
 23             <TextView
 24                 android:id="@+id/textView"
 25                 android:layout_width="match_parent"
 26                 android:layout_height="50dp"
 27                 android:background="@android:color/black"
 28                 android:gravity="center_vertical"
 29                 android:padding="10dp"
 30                 android:text="用户登陆"
 31                 android:textColor="@android:color/background_light"
 32                 android:textSize="24sp" />
 33         </LinearLayout>
 34 
 35         <LinearLayout
 36             android:layout_width="match_parent"
 37             android:layout_height="match_parent"
 38             android:layout_margin="3dp"
 39             android:orientation="vertical">
 40 
 41             <LinearLayout
 42                 android:layout_width="match_parent"
 43                 android:layout_height="match_parent"
 44                 android:background="@android:color/background_light"
 45                 android:orientation="horizontal">
 46 
 47                 <TextView
 48                     android:id="@+id/textView2"
 49                     android:layout_width="wrap_content"
 50                     android:layout_height="wrap_content"
 51                     android:layout_weight="1"
 52                     android:text="用户名:"
 53                     android:textSize="24sp" />
 54 
 55                 <EditText
 56                     android:id="@+id/username"
 57                     android:layout_width="wrap_content"
 58                     android:layout_height="wrap_content"
 59                     android:layout_weight="1"
 60                     android:ems="10"
 61                     android:hint="请填写登陆账号"
 62                     android:inputType="textPersonName" />
 63             </LinearLayout>
 64 
 65             <LinearLayout
 66                 android:layout_width="match_parent"
 67                 android:layout_height="match_parent"
 68                 android:background="@android:color/background_light"
 69                 android:orientation="horizontal">
 70 
 71                 <TextView
 72                     android:id="@+id/textView3"
 73                     android:layout_width="wrap_content"
 74                     android:layout_height="wrap_content"
 75                     android:layout_weight="1"
 76                     android:text="密码:  "
 77                     android:textSize="24sp" />
 78 
 79                 <EditText
 80                     android:id="@+id/passwrod"
 81                     android:layout_width="wrap_content"
 82                     android:layout_height="wrap_content"
 83                     android:layout_weight="1"
 84                     android:ems="10" />
 85             </LinearLayout>
 86 
 87         </LinearLayout>
 88 
 89         <LinearLayout
 90             android:layout_width="match_parent"
 91             android:layout_height="match_parent"
 92             android:layout_margin="3dp"
 93             android:background="@android:color/darker_gray"
 94             android:orientation="horizontal">
 95 
 96             <Button
 97                 android:id="@+id/login"
 98                 android:layout_width="wrap_content"
 99                 android:layout_height="wrap_content"
100                 android:layout_weight="1"
101                 android:text="登陆" />
102 
103             <Button
104                 android:id="@+id/cancel"
105                 android:layout_width="wrap_content"
106                 android:layout_height="wrap_content"
107                 android:layout_weight="1"
108                 android:text="取消" />
109         </LinearLayout>
110     </LinearLayout>
111 </LinearLayout>
activity_login.xml
 1 package com.example.login2;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 
10 public class LoginActivity extends AppCompatActivity {
11 
12     private EditText username;
13     private EditText password;
14     private Button login;
15     private Button cancel;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_login);
20         username =(EditText)findViewById(R.id.username);
21         password =(EditText)findViewById(R.id.passwrod);
22         login = (Button)findViewById(R.id.login);
23         cancel = (Button)findViewById(R.id.cancel);
24         login.setOnClickListener(new View.OnClickListener() {
25             @Override
26             public void onClick(View v) {
27                 String username1 = username.getText().toString();
28                 String password1 = password.getText().toString();
29                 Intent intent = new Intent(LoginActivity.this, MainActivity.class);
30                 if(username1.equals("admin") && password1.equals("12345"))
31                 {
32 
33                     intent.putExtra("isLogin",true);
34                     intent.putExtra("username",username1);
35                 }
36                 else
37                 {
38                     intent.putExtra("isLogin",false);
39                 }
40                 startActivity(intent);
41             }
42         });
43         cancel.setOnClickListener(new View.OnClickListener() {
44             @Override
45             public void onClick(View v) {
46                 finish();
47             }
48         });
49     }
50 }
LoginActivity.java
 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     <TextView
10         android:id="@+id/tv"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Hello World!"
14         app:layout_constraintBottom_toBottomOf="parent"
15         app:layout_constraintLeft_toLeftOf="parent"
16         app:layout_constraintRight_toRightOf="parent"
17         app:layout_constraintTop_toTopOf="parent" />
18 
19 </android.support.constraint.ConstraintLayout>
activity_main.xml
 1 package com.example.login2;
 2 
 3 import android.content.Intent;
 4 import android.support.annotation.Nullable;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 import android.widget.TextView;
11 
12 public class MainActivity extends AppCompatActivity {
13 
14     private TextView tv;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         tv = (TextView)findViewById(R.id.tv);
20         Intent intent = getIntent();
21         boolean isLogin = intent.getBooleanExtra("isLogin",false);
22         if(isLogin){
23             String username = intent.getStringExtra("username");
24             tv.setText(username+",欢迎您登陆到我们系统!");
25         }
26         else
27         {
28             tv.setText("您输入的信息有错,请重新登陆!");
29         }
30     }
31 }
MainActivity.java

 

Guess you like

Origin www.cnblogs.com/jdxb/p/10929904.html