1.创建程序activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:id="@+id/regist_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:orientation="horizontal"> <TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="用户名:"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的用户名"
android:textSize="14dp"/> </LinearLayout>
<LinearLayout
android:id="@+id/regist_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/regist_username"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"> <TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="密 码:"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码"
android:inputType="textPassword"
android:textSize="14dp"/>
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/regist_password"
android:layout_marginLeft="30dp"
android:contentDescription="性别"
android:orientation="horizontal"> <RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup> <Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="提交用户信息"/> </RelativeLayout>

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

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

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20dp"/> <TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20dp"/> <TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20dp"/>
</LinearLayout>

3.编写界面交互代码

Main:

package passdata.itcast.cn.zhuce;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton; public class Main extends Activity { private RadioButton manRadio;
private RadioButton womanRadio;
private EditText et_password;
private Button btn_send;
private EditText et_name; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_name=(EditText) findViewById(R.id.et_name);
et_password=(EditText) findViewById(R.id.et_password);
btn_send=(Button) findViewById(R.id.btn_send); manRadio=(RadioButton) findViewById(R.id.radioMale);
womanRadio=(RadioButton) findViewById(R.id.radioFemale); btn_send=(Button) findViewById(R.id.btn_send); btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
passDate();
}
});
} public void passDate(){ Intent intent=new Intent(this, Activity02.class); intent.putExtra("name", et_name.getText().toString().trim());
intent.putExtra("password", et_password.getText().toString().trim()); String str=""; if(manRadio.isChecked()){ str="男";
}
else if(womanRadio.isChecked()){ str="女";
} intent.putExtra("sex", str);
startActivity(intent);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}

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

Activity02:

package passdata.itcast.cn.zhuce;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by wanglaoda on 15-7-27.
*/
public class Activity02 extends Activity{ private TextView tv_name, tv_password, tv_sex; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
setContentView(R.layout.activity02); Intent intent=getIntent(); String name=intent.getStringExtra("name");
String password=intent.getStringExtra("password");
String sex=intent.getStringExtra("sex"); tv_name=(TextView) findViewById(R.id.tv_name);
tv_password=(TextView) findViewById(R.id.tv_password);
tv_sex=(TextView) findViewById(R.id.tv_sex); tv_name.setText("username:"+name);
tv_password.setText("密 码:"+password);
tv_sex.setText("性 别:"+sex);
}
}

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

4.清单文件配置:

<?xml version="1.0" encoding="utf-8"?

>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="passdata.itcast.cn.zhuce" > <application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="填写用户信息" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity02"
android:label="展示用户信息">
</activity>
</application> </manifest>

须要注意的是,android:label属性是用来指定显示在标题栏上的名称的,假设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. spring RestTemplate 实例(NameValuePair)

    第一种: public List<NameValuePair> getThirdsysPermissionRest(String url,ThirdsysFuncpDTO thirdsys ...

  2. c++:数据类型的推断type_traits

    //推断左值右值引用 void main() { int i(10);//i是左值 有内存实体 int &ri(i); int &&rri(i + 5);//右值引用 cout ...

  3. iOS UI08_UITableView

    (http://img.blog.csdn.net/20150808103801391) // // MainViewController.m // UI08_UITableView // // Cr ...

  4. MongoDB 系列(一) C# 类似EF语法简单封装

    之前写过一篇关于MongoDB的封装 发现太过繁琐 于是打算从新写一篇简易版 1:关于MongoDB的安装请自行百度,进行权限认证的时候有一个小坑,3.0之后授权认证方式默认的SCRAM-SHA-1模 ...

  5. 今天犯的一个错误,导致method GET must not have a request body

    事件经过: 1.在本地机器运行完全正常的程序,手动人工发包到测试环境上,后台日志频频报method GET must not have a request body. 2.使用postman发送pos ...

  6. CSS3的常用属性(二)

    边框 边框圆角 border-radius: 100px 每个角可以设置两个值,x和y 补充: 可分别设置长,短半径,以“/”进行分隔,遵循顺时针的顺序,“/”之前为横轴半径,“/”之后为纵轴半径,如 ...

  7. 005.JMS可靠性机制

    1. 消息接收确认 JMS消息只有在被确认之后,才认为已经被成功地消费了.消息的成功消费通常包含三个阶段: 客户接收消息 客户处理消息 消息被确认 在事务性会话中,当一个事务被提交的时候,确认自动发生 ...

  8. MySQL学习(一)——启动和登录MySql遇到的问题及解决

    1.MySQL使用命令行启动时报错“发生系统错误 5”,如下: 原因:未用管理员身份运行cmd程序 解决方案:在开始菜单里找到命令提示符,右击选择以管理员身份运行 2.登录时报错“提示mysql不是内 ...

  9. javascript 精确加减乘除

    最近一个项目中要使用 JS 实现自动计算的功能,本以为只是实现简单的加.减.乘.除就可以了,于是三下五除二做完了. 正当我窃喜的时候,发现问题了... 进行一些浮点数运算时,计算结果都是让我大跌眼镜啊 ...

  10. .NET深入解析LINQ框架2

    1].开篇介绍 在开始看本篇文章之前先允许我打断一下各位的兴致.其实这篇文章本来是没有打算加“开篇介绍”这一小节的,后来想想还是有必要反馈一下读者的意见.经过前三篇文章的详细讲解,我们基本上对LINQ ...