• 安卓开发Android studio学习笔记14:用户注册登录(案例演示)


    第一步:配置activity_information.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:gravity="center"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtName"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvGender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/gender"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtGender"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvAge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/age"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtAge"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/phone"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtPhone"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="phone"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/email"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtEmail"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvHomePage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/home_page"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtHomePage"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textUri"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvMemo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/memo"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtMemo"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:lines="4" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <Button
                android:id="@+id/btnRegister"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:onClick="doRegister"
                android:text="@string/register" />
    
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="80dp"
                android:layout_marginLeft="10dp"
                android:layout_height="wrap_content"
                android:onClick="doCancel"
                android:text="@string/cancel" />
        LinearLayout>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182

    第二步:配置activity_registration.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:gravity="center"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtName"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvGender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/gender"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtGender"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvAge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/age"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtAge"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/phone"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtPhone"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="phone"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/email"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtEmail"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvHomePage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/home_page"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtHomePage"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textUri"
                android:singleLine="true" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <TextView
                android:id="@+id/tvMemo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/memo"
                android:textColor="#000000"
                android:textSize="16sp" />
    
            <EditText
                android:id="@+id/edtMemo"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:lines="4" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <Button
                android:id="@+id/btnRegister"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:onClick="doRegister"
                android:text="@string/register" />
    
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="80dp"
                android:layout_marginLeft="10dp"
                android:layout_height="wrap_content"
                android:onClick="doCancel"
                android:text="@string/cancel" />
        LinearLayout>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182

    第三步:配置strings.xml

    <resources>
        <string name="app_name">用户注册string>
        <string name="name">姓名:string>
        <string name="gender">性别:string>
        <string name="age">年龄:string>
        <string name="phone">电话:string>
        <string name="email">邮箱:string>
        <string name="home_page">主页:string>
        <string name="memo">备注:string>
        <string name="register">注册string>
        <string name="cancel">取消string>
    resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    第四步:配置InformationActivity

    package com.example.mylogin9;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class InformationActivity extends AppCompatActivity {
    
        private TextView tvName;
        private TextView tvGender;
        private TextView tvAge;
        private TextView tvPhone;
        private TextView tvEmail;
        private TextView tvHomePage;
        private TextView tvMemo;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // 利用布局文件设置用户界面
            setContentView(R.layout.activity_information);
    
            // 通过资源标识获得控件示例
            tvName = (TextView) findViewById(R.id.tvName);
            tvGender = (TextView) findViewById(R.id.tvGender);
            tvAge = (TextView) findViewById(R.id.tvAge);
            tvPhone = (TextView) findViewById(R.id.tvPhone);
            tvEmail = (TextView) findViewById(R.id.tvEmail);
            tvHomePage = (TextView) findViewById(R.id.tvHomePage);
            tvMemo = (TextView) findViewById(R.id.tvMemo);
    
            // 获得意图
            Intent intent = getIntent();
    
            if (intent != null) {
                // 获得意图携带的数据包
                Bundle bundle = intent.getExtras();
    
                // 从数据包里按键取值
                String strName = bundle.getString("name");
                String strGender = bundle.getString("gender");
                String strAge = bundle.getString("age");
                String strPhone = bundle.getString("phone");
                String strEmail = bundle.getString("email");
                String strHomePage = bundle.getString("home_page");
                String strMemo = bundle.getString("memo");
    
                // 设置各个标签的内容
                tvName.setText("姓名:" + strName);
                tvGender.setText("性别:" + strGender);
                tvAge.setText("年龄:" + strAge);
                tvPhone.setText("电话:" + strPhone);
                tvEmail.setText("邮箱:" + strEmail);
                tvHomePage.setText("主页:" + strHomePage);
                tvMemo.setText("备注:" + strMemo);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    第五步:配置RegistrationActivity

    package com.example.mylogin9;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    
    public class RegistrationActivity extends AppCompatActivity {
    
        private EditText edtName;
        private EditText edtGender;
        private EditText edtAge;
        private EditText edtPhone;
        private EditText edtEmail;
        private EditText edtHomePage;
        private EditText edtMemo;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 利用布局资源文件设置用户界面
            setContentView(R.layout.activity_registration);
    
            // 通过资源标识符获得控件实例
            edtName = findViewById(R.id.edtName);
            edtGender = findViewById(R.id.edtGender);
            edtAge = findViewById(R.id.edtAge);
            edtPhone = findViewById(R.id.edtPhone);
            edtEmail = findViewById(R.id.edtEmail);
            edtHomePage = findViewById(R.id.edtHomePage);
            edtMemo = findViewById(R.id.edtMemo);
        }
    
        /**
         * 注册按钮单击事件处理方法
         *
         * @param view
         */
        public void doRegister(View view) {
            // 获取用户输入数据
            String strName = edtName.getText().toString();
            String strGender = edtGender.getText().toString();
            String strAge = edtAge.getText().toString();
            String strPhone = edtPhone.getText().toString();
            String strEmail = edtEmail.getText().toString();
            String strHomePage = edtHomePage.getText().toString();
            String strMemo = edtMemo.getText().toString();
    
            // 将各项输入数据打包
            Bundle data = new Bundle();
            data.putString("name",strName);
            data.putString("gender", strGender);
            data.putString("age", strAge);
            data.putString("phone", strPhone);
            data.putString("email", strEmail);
            data.putString("home_page", strHomePage);
            data.putString("memo", strMemo);
    
            // 创建意图,指定起始组件与目标组件
            Intent intent = new Intent(RegistrationActivity.this, InformationActivity.class);
            // 利用意图携带数据包
            intent.putExtras(data);
            // 按意图启动目标窗口
            startActivity(intent);
        }
    
        /**
         * 取消按钮单击事件处理方法
         *
         * @param view
         */
        public void doCancel(View view) {
            finish();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    第六步:运行结果代码截图

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    谁不是一边升学求职,一边死在路上
    冰点还原精灵DeepFreeze重启后图标不见了
    JedisPool
    lvs集群
    python-pptx解析pptx模板
    前端面试题2
    SpringBoot+@Async=王炸
    Mand Mobile - 滴滴出品的适用于金融项目的 Vue 移动端 UI 组件库,免费开源、灵活快速、丰富实用
    开发工具系列IDEA:配置注释自动生成
    推荐收藏!商汤智能座舱算法岗面试题7道(含解析)!
  • 原文地址:https://blog.csdn.net/m0_62491934/article/details/127510694