Android实现注册页面(携带数据包跳转)

安卓学习:实现注册页面输入数据,点击注册按钮跳转到另一个页面并显示输入信息
效果:
Android实现注册页面(携带数据包跳转)
文章图片

实现
1.创建安卓文件
Android实现注册页面(携带数据包跳转)
文章图片

2.创建注册界面,勾选为启动页
Android实现注册页面(携带数据包跳转)
文章图片

Android实现注册页面(携带数据包跳转)
文章图片

3.编写代码
启动界面activity_register11.xml


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; @Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onClick(View view) {//关闭窗口finish(); }}); }}

activity_main.xml

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; @Overrideprotected 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); }}}

【Android实现注册页面(携带数据包跳转)】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读