时间:2022-12-02 11:43:06 | 栏目:Android代码 | 点击:次
安卓学习:实现注册页面输入数据,点击注册按钮跳转到另一个页面并显示输入信息
效果:
实现
1.创建安卓文件
2.创建注册界面,勾选为启动页
3.编写代码
启动界面activity_register11.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bk024" android:gravity="center" android:padding="15sp" android:orientation="vertical" tools:context=".register11"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFF00" android:text="@string/name" android:textSize="25sp"/> <EditText android:id="@+id/edtName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvSex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFF00" android:text="@string/sex" android:textSize="25sp"/> <EditText android:id="@+id/edtSex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvAge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFF00" android:text="@string/age" android:textSize="25sp"/> <EditText android:id="@+id/edtAge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvPhone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFF00" android:text="@string/phone" android:textSize="25sp"/> <EditText android:id="@+id/edtPhone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvEmail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFF00" android:text="@string/email" android:textSize="25sp"/> <EditText android:id="@+id/edtEmail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvHomepage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/homepage" android:textColor="#FFFF00" android:textSize="25sp"/> <EditText android:id="@+id/edtHomepage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:id="@+id/tvRemark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFF00" android:text="@string/remark" android:textSize="25sp"/> <EditText android:id="@+id/edtRemark" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="12" android:singleLine="true"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/btnRegister" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="30dp" android:paddingRight="30dp" android:background="@color/danlv" android:textColor="#ffffff" android:text="@string/register" android:layout_marginRight="10dp" android:textSize="20sp"/> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="30dp" android:paddingRight="30dp" android:background="@color/danlv" android:text="@string/cancel" android:textSize="20sp" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout>
register11.java
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; public class register11 extends AppCompatActivity { private EditText edtName; private EditText edtSex; private EditText edtAge; private EditText edtPhone; private EditText edtEmail; private EditText edtHomepage; private EditText edtRemark; private Button btnRegister; private Button btnCancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register11); //活动栏图标 ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayUseLogoEnabled(true); actionBar.setLogo(R.mipmap.icon); //获取控件实例 edtName = findViewById(R.id.edtName); edtSex = findViewById(R.id.edtSex); edtAge = findViewById(R.id.edtAge); edtPhone = findViewById(R.id.edtPhone); edtEmail = findViewById(R.id.edtEmail); edtHomepage = findViewById(R.id.edtHomepage); edtRemark = findViewById(R.id.edtRemark); btnRegister = findViewById(R.id.btnRegister); btnCancel = findViewById(R.id.btnCancel); //给注册按钮注册监听器 btnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //保存用户输入数据 String strName=edtName.getText().toString().trim();//trim()去除用户在前面输入的空格 String strSex=edtSex.getText().toString().trim(); String strAge=edtAge.getText().toString().trim();//trim()去除用户在前面输入的空格 String strPhone=edtPhone.getText().toString().trim();//trim()去除用户在前面输入的空格 String strEmail=edtEmail.getText().toString().trim(); String strHomepage=edtHomepage.getText().toString().trim();//trim()去除用户在前面输入的空格 String strRemark=edtRemark.getText().toString().trim(); //利用吐司提示用户注册情况 Toast.makeText(register11.this, "注册成功", Toast.LENGTH_SHORT).show();//LENGTH_SHORT表示登录时间长度 //创建显示意图 Intent intent = new Intent(register11.this, MainActivity.class); //创建数据包封装数据(封装) Bundle date = new Bundle(); date.putString("name", strName); date.putString("sex", strSex); date.putString("age", strAge); date.putString("phone", strPhone); date.putString("email", strEmail); date.putString("homepage", strHomepage); date.putString("remark", strRemark); //通过意图携带数据包 intent.putExtras(date); //按照意图启动目标组件 startActivity(intent); } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //关闭窗口 finish(); } }); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/bk028" tools:context=".MainActivity"> <TextView android:id="@+id/tvMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " android:textSize="25dp" android:textColor="#ff2233" /> </LinearLayout>
ActivityMain.java
import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { protected TextView tvMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过资源标识符获取控件实例 tvMessage = findViewById(R.id.tvMessage); //获取意图 Intent intent = getIntent(); //判断意图是否为空 if (intent != null) { //获取意图携带数据包(封装) Bundle date = intent.getExtras(); String name = date.getString("name"); String sex = date.getString("sex"); String age = date.getString("age"); String phone = date.getString("phone"); String email = date.getString("email"); String homepage = date.getString("homepage"); String remark = date.getString("remark"); //拼接用户信息 String message = "注册成功!\n姓名:" + name + "\n性别:" + sex + "\n年龄:" + age + "\n电话:" + phone + "\n邮箱:" + email + "\n主页:" + homepage + "\n备注:" + remark; //设置标签属性,显示用户信息 tvMessage.setText(message); } } }