Android Studio实现简单登录注册界面{学习记录}

第一步:新建项目,并选择Java语言

第二步:实现登录界面

              2.1在AndroidStudioProjects/MyApplication/app/src/main/res/layout目录下新建布局文件 login.xml

                注:login是文件名可自行修改,但是需要同步修改和其关联文件中的名称

              2.2打开login.xml文件并点击Design选项

           界面如下:

                      2.3从Palette界面拖动相关控件到Component Tree

               如下图:

 

 

 

相关代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.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=".LoginActivity"
 8     android:padding="20dp">
 9 
10     <LinearLayout
11         android:layout_width="match_parent"
12         android:layout_height="match_parent"
13         android:orientation="vertical"
14         tools:layout_editor_absoluteX="20dp"
15         tools:layout_editor_absoluteY="20dp">
16 
17         <LinearLayout
18             android:layout_width="match_parent"
19             android:layout_height="80dp"
20             android:orientation="horizontal">
21 
22             <TextView
23                 android:id="@+id/tvuser"
24                 android:layout_width="110dp"
25                 android:layout_height="wrap_content"
26                 android:text="用户名" />
27 
28             <EditText
29                 android:id="@+id/user"
30                 android:layout_width="251dp"
31                 android:layout_height="wrap_content"
32                 android:ems="10"
33                 android:hint="请输入用户名"
34                 android:inputType="textPersonName"
35                 android:minHeight="48dp" />
36         </LinearLayout>
37 
38         <LinearLayout
39             android:layout_width="match_parent"
40             android:layout_height="80dp"
41             android:orientation="horizontal">
42 
43             <TextView
44                 android:id="@+id/tvpwo"
45                 android:layout_width="111dp"
46                 android:layout_height="wrap_content"
47                 android:text="密码" />
48 
49             <EditText
50                 android:id="@+id/pwo"
51                 android:layout_width="259dp"
52                 android:layout_height="wrap_content"
53                 android:ems="10"
54                 android:hint="请输入密码"
55                 android:inputType="textPassword"
56                 android:minHeight="48dp" />
57         </LinearLayout>
58 
59         <Button
60             android:id="@+id/button5"
61             android:layout_width="match_parent"
62             android:layout_height="wrap_content"
63             android:text="登录"
64             tools:ignore="MissingConstraints,OnClick"/>
65 
66         <Button
67             android:id="@+id/buttonR"
68             android:layout_width="match_parent"
69             android:layout_height="wrap_content"
70             android:text="注册" />
71 
72         <TextView
73             android:id="@+id/tvRC"
74             android:layout_width="match_parent"
75             android:layout_height="wrap_content" />
76 
77     </LinearLayout>
78 
79 </androidx.constraintlayout.widget.ConstraintLayout>

        2.3在AndroidStudioProjects/MyApplication/app/src/main/java/com/example/myapplication目录中创建名为LoginActivity.java的Java文件

                                  如右图所示:

 

      2.4LoginActivity.java中代码如下

 1 package com.example.myapplication;
 2 import androidx.appcompat.app.AppCompatActivity;
 3 import android.annotation.SuppressLint;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.widget.TextView;
10 import android.widget.Toast;
11 
12 public class LoginActivity extends AppCompatActivity
13 {
14     @SuppressLint("WrongViewCast")
15     EditText user,pwo;
16     Button button5,buttonR;
17     TextView tvC;
18     @SuppressLint("WrongViewCast")
19     @Override
20     protected void onCreate(Bundle savedInstanceState)
21     {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.login);
24         initView();
25 
26         button5.setOnClickListener(new MyButtonListener());
27     }
28         @SuppressLint("WrongViewCast")
29         private void initView ()
30         {
31             user = findViewById(R.id.user);
32             pwo = findViewById(R.id.pwo);
33             tvC = findViewById(R.id.tvRC);
34             button5 = findViewById(R.id.button5);
35             //点击注册跳转
36             buttonR = findViewById(R.id.buttonR);
37             buttonR.setOnClickListener(new View.OnClickListener()
38             {
39                 @Override
40                 public void onClick(View v)
41                 {
42                     Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
43                     startActivity(intent);
44                 }
45             });
46         }
47         //显示登录信息
48     public void Login(View view)
49     {
50 
51     }
52 
53     class MyButtonListener implements View.OnClickListener
54     {
55             public void onClick(View view)
56             {
57                 String name=user.getText().toString();
58                 String pow=pwo.getText().toString();
59                 tvC.setText("用户名:"+name+"密码:"+pow+"登录");
60 
61             }
62     }
63 }

第三步:注册界面 同登录界面方法类似

         3.1在AndroidStudioProjects/MyApplication/app/src/main/res/layout目录下新建布局文件 register.xml

         3.2打开register.xml文件并点击Design选项

         3.3从Palette界面拖动相关控件到Component Tree

         3.4代码如下:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <androidx.constraintlayout.widget.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=".LoginActivity"
  8     android:padding="20dp">
  9 
 10     <ScrollView
 11         android:layout_width="match_parent"
 12         android:layout_height="match_parent">
 13 
 14         <LinearLayout
 15             android:layout_width="match_parent"
 16             android:layout_height="wrap_content"
 17             android:orientation="vertical">
 18 
 19             <LinearLayout
 20                 android:layout_width="match_parent"
 21                 android:layout_height="80dp"
 22                 android:orientation="horizontal">
 23 
 24                 <TextView
 25                     android:id="@+id/tvuser"
 26                     android:layout_width="0dp"
 27                     android:layout_height="wrap_content"
 28                     android:layout_weight="1"
 29                     android:text="用户名" />
 30 
 31                 <EditText
 32                     android:id="@+id/user"
 33                     android:layout_width="0dp"
 34                     android:layout_height="wrap_content"
 35                     android:layout_weight="4"
 36                     android:ems="10"
 37                     android:hint="请输入用户名"
 38                     android:inputType="textPersonName"
 39                     android:minHeight="48dp" />
 40             </LinearLayout>
 41 
 42             <LinearLayout
 43                 android:layout_width="match_parent"
 44                 android:layout_height="80dp"
 45                 android:orientation="horizontal">
 46 
 47                 <TextView
 48                     android:id="@+id/tvpwo"
 49                     android:layout_width="0dp"
 50                     android:layout_height="wrap_content"
 51                     android:layout_weight="1"
 52                     android:text="密码" />
 53 
 54                 <EditText
 55                     android:id="@+id/pwo"
 56                     android:layout_width="0dp"
 57                     android:layout_height="wrap_content"
 58                     android:layout_weight="4"
 59                     android:ems="10"
 60                     android:hint="请输入密码"
 61                     android:inputType="textPassword"
 62                     android:minHeight="48dp" />
 63             </LinearLayout>
 64 
 65             <LinearLayout
 66                 android:layout_width="match_parent"
 67                 android:layout_height="80dp"
 68                 android:orientation="horizontal">
 69 
 70                 <TextView
 71                     android:id="@+id/tvrepwo"
 72                     android:layout_width="0dp"
 73                     android:layout_height="wrap_content"
 74                     android:layout_weight="1"
 75                     android:text="确认密码" />
 76 
 77                 <EditText
 78                     android:id="@+id/rpw"
 79                     android:layout_width="wrap_content"
 80                     android:layout_height="wrap_content"
 81                     android:layout_weight="1"
 82                     android:ems="10"
 83                     android:hint="请确认密码"
 84                     android:inputType="textPassword"
 85                     android:minHeight="48dp" />
 86             </LinearLayout>
 87 
 88             <LinearLayout
 89                 android:layout_width="match_parent"
 90                 android:layout_height="80dp"
 91                 android:orientation="horizontal">
 92 
 93                 <TextView
 94                     android:id="@+id/tvname"
 95                     android:layout_width="0dp"
 96                     android:layout_height="wrap_content"
 97                     android:layout_weight="1"
 98                     android:text="姓名" />
 99 
100                 <EditText
101                     android:id="@+id/uname"
102                     android:layout_width="wrap_content"
103                     android:layout_height="wrap_content"
104                     android:layout_weight="1"
105                     android:ems="10"
106                     android:hint="请输入您的姓名"
107                     android:inputType="textPersonName"
108                     android:minHeight="48dp" />
109 
110             </LinearLayout>
111 
112             <LinearLayout
113                 android:layout_width="match_parent"
114                 android:layout_height="30dp"
115                 android:orientation="horizontal">
116 
117                 <TextView
118                     android:id="@+id/sex"
119                     android:layout_width="145dp"
120                     android:layout_height="match_parent"
121                     android:layout_weight="1"
122                     android:text="性别"
123                     tools:text="性别" />
124 
125                 <RadioGroup
126                     android:id="@+id/sexq"
127                     android:layout_width="match_parent"
128                     android:layout_height="match_parent"
129                     android:layout_weight="1"
130                     android:orientation="horizontal">
131 
132                     <RadioButton
133                         android:id="@+id/ButtonM"
134                         android:layout_width="0dp"
135                         android:layout_height="wrap_content"
136                         android:layout_weight="1"
137                         android:text="男"
138                         tools:ignore="TouchTargetSizeCheck" />
139 
140                     <RadioButton
141                         android:id="@+id/ButtonW"
142                         android:layout_width="0dp"
143                         android:layout_height="wrap_content"
144                         android:layout_weight="1"
145                         android:text="女"
146                         tools:ignore="TouchTargetSizeCheck" />
147                 </RadioGroup>
148 
149             </LinearLayout>
150 
151             <CheckBox
152                 android:id="@+id/checkBox3"
153                 android:layout_width="match_parent"
154                 android:layout_height="wrap_content"
155                 android:text="已阅读会员协议" />
156 
157             <Button
158                 android:id="@+id/buttonR"
159                 android:layout_width="match_parent"
160                 android:layout_height="50dp"
161                 android:text="注册" />
162 
163             <Button
164                 android:id="@+id/buttonB"
165                 android:layout_width="match_parent"
166                 android:layout_height="wrap_content"
167                 android:text="返回" />
168 
169             <TextView
170                 android:id="@+id/tvRC"
171                 android:layout_width="match_parent"
172                 android:layout_height="wrap_content" />
173         </LinearLayout>
174     </ScrollView>
175 
176 </androidx.constraintlayout.widget.ConstraintLayout>

 

       3.5在AndroidStudioProjects/MyApplication/app/src/main/java/com/example/myapplication目录中创建名为RegisterActivity.java的Java文件

                如右图:

      3.6在RegisterActivity.java编写代码

            代码如下:

 1 package com.example.myapplication;
 2 
 3 import android.annotation.SuppressLint;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.CheckBox;
 8 import android.widget.EditText;
 9 import android.widget.RadioGroup;
10 import android.widget.Toast;
11 import androidx.appcompat.app.AppCompatActivity;
12 public class RegisterActivity extends AppCompatActivity {
13     EditText user, pwo, rpw, uname;
14     Button buttonR, buttonB;
15     RadioGroup sexq;
16     CheckBox checkBox3;
17     String sex="男";
18 
19     @SuppressLint("WrongViewCast")
20     protected void onCreate(Bundle savedInstanceState)
21     {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.register);
24         {
25             buttonB = findViewById(R.id.buttonB);
26             buttonR = findViewById(R.id.buttonR);
27             uname = findViewById(R.id.uname);
28             pwo = findViewById(R.id.pwo);
29             rpw = findViewById(R.id.rpw);
30             user = findViewById(R.id.user);
31             sexq = findViewById(R.id.sexq);
32             checkBox3 = findViewById(R.id.checkBox3);
33             sexq.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
34             {
35                 @Override
36                 public void onCheckedChanged(RadioGroup radioGroup, int i)
37                 {
38                     switch (radioGroup.getCheckedRadioButtonId())
39                     {
40                         case R.id.ButtonM:
41                             sex = "男";
42                             break;
43                         case R.id.ButtonW:
44                             sex = "女";
45                             break;
46                     }
47                 }
48             });
49             buttonR.setOnClickListener(new View.OnClickListener()
50             {
51                 @Override
52 
53                 public void onClick(View view)
54                 {
55                     if (checkBox3.isChecked())
56                     {
57                         if (rpw.getText().toString().trim().equals("") || uname.getText().toString().trim().equals("") || pwo.getText().toString().trim().equals("") || user.getText().toString().trim().equals(""))
58                         {
59                             Toast.makeText(RegisterActivity.this, "请填写所有内容", Toast.LENGTH_LONG).show();
60                             return;
61                         }
62                         if (!rpw.getText().toString().trim().equals(pwo.getText().toString().trim()))
63                         {
64                             Toast.makeText(RegisterActivity.this, "两次密码输入不一致,请重新输入", Toast.LENGTH_LONG).show();
65                             return;
66                         }
67                         String res = "欢迎你,你的注册信息如下:\n" + "用户名:" + user.getText().toString().trim() + "\n密码:" + pwo.getText().toString().trim() + "\n姓名:" + uname.getText().toString().trim() + "\n性别" + sex + "\n";
68                         Toast.makeText(RegisterActivity.this, res, Toast.LENGTH_LONG).show();
69                     }
70                     else
71                     {
72                         Toast.makeText(RegisterActivity.this, "请阅读会员协议并勾选", Toast.LENGTH_LONG).show();
73                     }
74 
75                 }
76             });
77             //点击返回跳转登录界面
78             buttonB = findViewById(R.id.buttonB);
79             buttonB.setOnClickListener(new View.OnClickListener()
80             {
81                 @Override
82                 public void onClick(View v)
83                 {
84 
85                     finish();
86                 }
87             });
88         }
89 
90     }
91 }

第四步:在AndroidStudioProjects/MyApplication/app/src/main目录的AndroidManifest.xml添加相关内容

              使LoginActivity.java和RegisterActivity.java两个文件可以运行在Android虚拟机中

注:由于本人初学者,暂时无法深入理解AndroidManifest.xml中信息,仅做学习记录

    代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.myapplication">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android:theme="@style/Theme.MyApplication">
12         <activity
13             android:name=".LoginActivity"
14             android:exported="true">
15             <intent-filter>
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <activity
22             android:name=".RegisterActivity"
23             android:exported="true">
24             <intent-filter>
25                 <action android:name="android.intent.action.MAIN" />
26 
27                 <category android:name="android.intent.category.LAUNCHER" />
28             </intent-filter>
29         </activity>
30     </application>
31 
32 </manifest>

 

最后

在虚拟机中运行效果如图:

 

posted @ 2022-03-15 12:08  ILEQ  阅读(19510)  评论(1编辑  收藏  举报