前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之TextView使用intent传递信息,实现注册界面功能示例

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

作者头像
砸漏
发布2020-11-05 10:31:48
8120
发布2020-11-05 10:31:48
举报
文章被收录于专栏:恩蓝脚本

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

使用intent在活动间传递值

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

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

效果图如下:

MainActivity 实现:

Java代码:

代码语言:javascript
复制
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布局文件:

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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程序设计有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档