1.创建程序activity_main:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5.  
  6. <LinearLayout
  7. android:id="@+id/regist_username"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_centerHorizontal="true"
  11. android:layout_marginLeft="10dp"
  12. android:layout_marginRight="10dp"
  13. android:layout_marginTop="22dp"
  14. android:orientation="horizontal">
  15.  
  16. <TextView
  17. android:layout_width="80dp"
  18. android:layout_height="wrap_content"
  19. android:gravity="right"
  20. android:paddingRight="5dp"
  21. android:text="用户名:"/>
  22. <EditText
  23. android:id="@+id/et_name"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:hint="请输入您的用户名"
  27. android:textSize="14dp"/>
  28.  
  29. </LinearLayout>
  30. <LinearLayout
  31. android:id="@+id/regist_password"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_below="@+id/regist_username"
  35. android:layout_centerHorizontal="true"
  36. android:layout_marginLeft="10dp"
  37. android:layout_marginRight="10dp"
  38. android:layout_marginTop="5dp"
  39. android:orientation="horizontal">
  40.  
  41. <TextView
  42. android:layout_width="80dp"
  43. android:layout_height="wrap_content"
  44. android:gravity="right"
  45. android:paddingRight="5dp"
  46. android:text="密 码:"/>
  47. <EditText
  48. android:id="@+id/et_password"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content"
  51. android:hint="请输入您的密码"
  52. android:inputType="textPassword"
  53. android:textSize="14dp"/>
  54. </LinearLayout>
  55. <RadioGroup
  56. android:id="@+id/radioGroup"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_below="@+id/regist_password"
  60. android:layout_marginLeft="30dp"
  61. android:contentDescription="性别"
  62. android:orientation="horizontal">
  63.  
  64. <RadioButton
  65. android:id="@+id/radioMale"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:checked="true"
  69. android:text="男"/>
  70. <RadioButton
  71. android:id="@+id/radioFemale"
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:text="女"/>
  75. </RadioGroup>
  76.  
  77. <Button
  78. android:id="@+id/btn_send"
  79. android:layout_width="wrap_content"
  80. android:layout_height="wrap_content"
  81. android:layout_below="@+id/radioGroup"
  82. android:layout_centerHorizontal="true"
  83. android:layout_marginTop="24dp"
  84. android:text="提交用户信息"/>
  85.  
  86. </RelativeLayout>

在上述代码中,定义了一个相对布局RelativeLayout,该布局中创建了一个EditText和一个Buttonbutton,分别用于输入内容和点击“提交用户信息”button进行数据传递。

2.创建接收数据Activity界面activity02:

  1. <?xml version="1.0" encoding="utf-8"?
  2.  
  3. >
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8.  
  9. <TextView
  10. android:id="@+id/tv_name"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:gravity="center"
  14. android:layout_marginTop="10dp"
  15. android:textSize="20dp"/>
  16.  
  17. <TextView
  18. android:id="@+id/tv_password"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:gravity="center"
  22. android:layout_marginTop="10dp"
  23. android:textSize="20dp"/>
  24.  
  25. <TextView
  26. android:id="@+id/tv_sex"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:gravity="center"
  30. android:layout_marginTop="10dp"
  31. android:textSize="20dp"/>
  32. </LinearLayout>

3.编写界面交互代码

Main:

  1. package passdata.itcast.cn.zhuce;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.RadioButton;
  13.  
  14. public class Main extends Activity {
  15.  
  16. private RadioButton manRadio;
  17. private RadioButton womanRadio;
  18. private EditText et_password;
  19. private Button btn_send;
  20. private EditText et_name;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26.  
  27. et_name=(EditText) findViewById(R.id.et_name);
  28. et_password=(EditText) findViewById(R.id.et_password);
  29. btn_send=(Button) findViewById(R.id.btn_send);
  30.  
  31. manRadio=(RadioButton) findViewById(R.id.radioMale);
  32. womanRadio=(RadioButton) findViewById(R.id.radioFemale);
  33.  
  34. btn_send=(Button) findViewById(R.id.btn_send);
  35.  
  36. btn_send.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. passDate();
  40. }
  41. });
  42. }
  43.  
  44. public void passDate(){
  45.  
  46. Intent intent=new Intent(this, Activity02.class);
  47.  
  48. intent.putExtra("name", et_name.getText().toString().trim());
  49. intent.putExtra("password", et_password.getText().toString().trim());
  50.  
  51. String str="";
  52.  
  53. if(manRadio.isChecked()){
  54.  
  55. str="男";
  56. }
  57. else if(womanRadio.isChecked()){
  58.  
  59. str="女";
  60. }
  61.  
  62. intent.putExtra("sex", str);
  63. startActivity(intent);
  64. }
  65.  
  66. @Override
  67. public boolean onCreateOptionsMenu(Menu menu) {
  68. // Inflate the menu; this adds items to the action bar if it is present.
  69. getMenuInflater().inflate(R.menu.menu_main, menu);
  70. return true;
  71. }
  72.  
  73. @Override
  74. public boolean onOptionsItemSelected(MenuItem item) {
  75. // Handle action bar item clicks here. The action bar will
  76. // automatically handle clicks on the Home/Up button, so long
  77. // as you specify a parent activity in AndroidManifest.xml.
  78. int id = item.getItemId();
  79.  
  80. //noinspection SimplifiableIfStatement
  81. if (id == R.id.action_settings) {
  82. return true;
  83. }
  84.  
  85. return super.onOptionsItemSelected(item);
  86. }
  87. }

在上述代码中,passDate()方法实现了获取用户输入数据,而且将Intent作为载体进行数据传递。

Activity02:

  1. package passdata.itcast.cn.zhuce;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. /**
  9. * Created by wanglaoda on 15-7-27.
  10. */
  11. public class Activity02 extends Activity{
  12.  
  13. private TextView tv_name, tv_password, tv_sex;
  14.  
  15. protected void onCreate(Bundle savedInstanceState){
  16.  
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity02);
  19.  
  20. Intent intent=getIntent();
  21.  
  22. String name=intent.getStringExtra("name");
  23. String password=intent.getStringExtra("password");
  24. String sex=intent.getStringExtra("sex");
  25.  
  26. tv_name=(TextView) findViewById(R.id.tv_name);
  27. tv_password=(TextView) findViewById(R.id.tv_password);
  28. tv_sex=(TextView) findViewById(R.id.tv_sex);
  29.  
  30. tv_name.setText("username:"+name);
  31. tv_password.setText("密 码:"+password);
  32. tv_sex.setText("性 别:"+sex);
  33. }
  34. }

在上述代码中,第20~32行代码通过getIntent()方法获取到Intent对象,然后通过该对象的getStringExtra()方法获取输入的username。并将得到的username绑定在TextView控件中进行显示。须要注意的是。getStringExtra(String str)方法传入的參数必须是Main中的intent.putExtra()方法中传入的key,否则会返回null。

4.清单文件配置:

  1. <?xml version="1.0" encoding="utf-8"?
  2. >
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.     package="passdata.itcast.cn.zhuce" >
  5.     <application
  6.         android:allowBackup="true"
  7.         android:label="@string/app_name"
  8.         android:icon="@mipmap/ic_launcher"
  9.         android:theme="@style/AppTheme" >
  10.         <activity
  11.             android:name=".Main"
  12.             android:label="填写用户信息" >
  13.             <intent-filter>
  14.                 <action android:name="android.intent.action.MAIN" />
  15.                 <category android:name="android.intent.category.LAUNCHER" />
  16.             </intent-filter>
  17.         </activity>
  18.         <activity
  19.             android:name=".Activity02"
  20.             android:label="展示用户信息">
  21.         </activity>
  22.     </application>
  23. </manifest>
  24.  
  25. 须要注意的是,androidlabel属性是用来指定显示在标题栏上的名称的,假设Activity设置了该属性。则跳到该Activity
    页面时的标题栏会显示在Activity中的配置名称。否则显示在Application中的配置名称。

Android笔记——Activity中的数据传递案例(用户注冊)的更多相关文章

  1. Android笔记——Activity中的回传数据案例(装备选择)

    1.创建程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  2. 【Android 复习】:从Activity中返回数据

    在实际的应用中,我们不仅仅要向Activity传递数据,而且要从Activity中返回数据,虽然返回数据和传递类似,也可以采用上一讲中的四种方式来传递数据,但是一般建议采用Intent对象的方式的来返 ...

  3. Android学习之Activity之间的数据传递

    Activity与Activity之间很多情况下都需要进行数据的传递,下面就用几个简单的例子来看一下. (一).一个Activity启动另一个Activity并将数据传递到这个Activity当中 思 ...

  4. Activity之间的数据传递-android学习之旅(四十七)

    activity之间的数据传递主要有两种,一种是直接发送数据,另一种接受新启动的activity返回的数据,本质是一样的 使用Bundle传递数据 Intent使用Bundle在activity之间传 ...

  5. Android Activity之间的数据传递

    1.向目标Activity传递数据: Intent intent=new Intent(this,Main2Activity.class); //可传递多种类型的数据 intent.putExtra( ...

  6. activity之间的数据传递方法

    1  基于消息的通信机制 Intent--------boudle,extra 用这种简单的形式,一般而言传递一些简单的类型是比较容易的,如int.string等 详细介绍下Intent机制 Inte ...

  7. Activity之间的数据传递

    最常用的Activity之间的数据传递. btnStartAty1.setOnClickListener(new View.OnClickListener() { @Override public v ...

  8. 从Activity中返回数据

    从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...

  9. [学习总结]5、Android的ViewGroup中事件的传递机制(二)

    下面是第一篇的连接 Android的ViewGroup中事件的传递机制(一) 关于onInterceptTouchEvent和onTouchEvent的详细解释. 1 public class Mai ...

随机推荐

  1. C/C++ Quick Sort Algorithm

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069 快速排序算法,由C.A. ...

  2. 一个简单搜索引擎的搭建过程(Solr+Nutch+Hadoop)

    最近,因为未来工作的需要,我尝试安装部署了分布式爬虫系统Nutch,并配置了伪分布式的Hadoop来存储爬取的网页结果,用solr来对爬下来的网页进行搜索.我主要通过参考网上的相关资料进行安装部署的. ...

  3. HDU 5410(2015多校10)-CRB and His Birthday(全然背包)

    题目地址:HDU 5410 题意:有M元钱,N种礼物,若第i种礼物买x件的话.会有Ai*x+Bi颗糖果,现给出每种礼物的单位价格.Ai值与Bi值.问最多能拿到多少颗糖果. 思路:全然背包问题. dp[ ...

  4. DataTables warning: table id=dataTable - Requested unknown parameter &#39;acceptId&#39; for row 0. For more

    重点内容 DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For ...

  5. C++ Primer Plus的若干收获--(九)

    这篇博文我接着上一篇来写,相同讲一些关于类的一些基础知识. 本篇将会继续使用上篇的股票类STock,这里给出接口 ifndef STOCKOO_H_ #define STOCKOO_H_ #inclu ...

  6. Hibernate关系映射中的注解

    一.@Entity 写在映射表的类上面,表示这是映射来的实体 二.@Id @Column(name = "fid", nullable = false) @Basic @Colum ...

  7. jqMobi(App Framework)入门学习(一)

    jqMobi(App Framework)入门学习(一) 1. 什么是jqMobi? jqMobi是由appMobi针对HTML5浏览器和移动设备开发的javascript框架.是个极其高速的查询选择 ...

  8. Linux就该这么学 20181007第十章Apache)

    参考链接https://www.linuxprobe.com/ /etc/httpd/conf/httpd.conf 主配置文件 SElinux域 ---服务功能的限制 SElinux安全上下文 -- ...

  9. BZOJ 2127 二元关系

    题意: 思路: 先把所有的值加起来 最小割割哪儿 就代表那个地方不选 一减 剩下的就是 最大值了 //By SiriusRen #include <cstdio> #include < ...

  10. 关于github里readme编辑的方法

    实验室的老师昨天改完论文发我后,说按照例子改.于是才发现github里readme编辑满满的极客思维. 看了一下csdn给的教程 https://blog.csdn.net/Kaitiren/arti ...