欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之TextView使用intent传递信息,实现注册界面功能示例

程序员文章站 2023-10-30 14:35:46
本文实例讲述了android开发之textview使用intent传递信息,实现注册界面功能。分享给大家供大家参考,具体如下: 使用intent在活动间传递值 首先是...

本文实例讲述了android开发之textview使用intent传递信息,实现注册界面功能。分享给大家供大家参考,具体如下:

使用intent在活动间传递值

首先是 mainactuvity 活动(注册界面 写完个人信息点击注册 )

跳转到 in 活动 (通过 intent 获得 mainactivity 中的信息 )

效果图如下:

Android开发之TextView使用intent传递信息,实现注册界面功能示例

mainactivity 实现:

java代码:

public class home extends appcompatactivity {
  //用于存放个人注册信息
  edittext user_name ;
  edittext user_code ;
  edittext user_year ;
  edittext user_birth ;
  edittext user_phone ;
  //注册按钮 点击跳转
  button button01 ;
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);//显示manlayout
    user_name = (edittext) findviewbyid(r.id.ed_name);
    user_code = (edittext) findviewbyid(r.id.ed_code);
    user_year = (edittext) findviewbyid(r.id.ed_year);
    user_birth = (edittext) findviewbyid(r.id.ed_birth);
    user_phone = (edittext) findviewbyid(r.id.ed_phone);
    button01 = (button) findviewbyid(r.id.bn_01);
    //通过 intent 实现活动间的信息传递
    button01.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent01 = new intent(home.this,in.class);
        intent01.putextra("name",user_name.gettext().tostring());
        intent01.putextra("code",user_code.gettext().tostring());
        intent01.putextra("year",user_year.gettext().tostring());
        intent01.putextra("birth",user_birth.gettext().tostring());
        intent01.putextra("phone",user_phone.gettext().tostring());
        startactivity(intent01);
      }
    });
  }
}

xml布局文件:

<tablelayout
  android:id="@+id/root"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:stretchcolumns="1">
  <tablerow>
    <textview
      android:id="@+id/tv_name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="用户名"
      android:textsize="16sp"/>
    <edittext
      android:id="@+id/ed_name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="请填写登陆账号"
      android:selectallonfocus="true"/>
  </tablerow>
  <tablerow>
    <textview
      android:id="@+id/tv_code"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="密码"
      android:textsize="16sp"/>
    <!--android:inputtype="numberpassword"表示只能接受数字密码-->
    <edittext
      android:id="@+id/ed_code"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputtype="numberpassword"/>
  </tablerow>
  <tablerow>
    <textview
      android:id="@+id/tv_year"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="年龄"
      android:textsize="16sp"/>
    <!--android:inputtype="numberpassword"表示是数值输入框-->
    <edittext
      android:id="@+id/ed_year"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputtype="number"/>
  </tablerow>
  <tablerow>
    <textview
      android:id="@+id/tv_birth"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="生日"
      android:textsize="16sp"/>
    <!--android:inputtype="numberpassword"表示日期输入框-->
    <edittext
      android:id="@+id/ed_birth"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputtype="date"/>
  </tablerow>
  <tablerow>
    <textview
      android:id="@+id/tv_phone"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="电话号码"
      android:textsize="16sp"/>
    <!--android:inputtype="numberpassword"表示电话号码输入框-->
    <edittext
      android:id="@+id/ed_phone"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:selectallonfocus="true"
      android:inputtype="phone"/>
  </tablerow>
  <button
    android:id="@+id/bn_01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="注册"/>
</tablelayout>

in 活动:

java代码:

public class in extends appcompatactivity {
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_in);
    //获得mainactivity传进来的数据
    intent intent01 = getintent();
    //放置传入的信息
    textview textview01 = (textview) findviewbyid(r.id.in_tv_01);
    textview01.settext( intent01.getstringextra("name") + "\n"
        + intent01.getstringextra("code") + "\n"
        + intent01.getstringextra("year") + "\n"
        + intent01.getstringextra("birth") + "\n"
        + intent01.getstringextra("phone") );
  }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".in">
  <!--//放置前一个活动传递进来的信息-->
  <textview
    android:id="@+id/in_tv_01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</android.support.constraint.constraintlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。