Activity间传递数据
1 基于消息的通信机制 Intent——–boudle , extra
用这种简单的形式,一般而言传递一些简单的类型是比较容易的,如int、string等
详细介绍下Intent机制
Intent包含两部分:
1 目的【action】——-要去到哪里去
2 内容【category、data】———-路上带些什么,区分性数据和内容性数据
简单数据传递:
String data = "HELLO";
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("extra_data", data);
startActivity(intent);
putExtra()方法中第一个参数接收Key,第二个参数接收Value。
在SecondActivity中取出传过来的数据
Intent intent = getIntent();
String data == intent.getStringExtra("extra_data");
Log.i("SecondActivity","data");
写一个Demo.
1.新建工程,MainActivity.class
package com.example.passdata;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends Activity {
private RadioButton raidoMale,radioFemale;
private EditText et_password,et_name;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitivity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_password = (EditText) findViewById(R.id.et_password);
raidoMale = (RadioButton) findViewById(R.id.radioMale);
radioFemale = (RadioButton) findViewById(R.id.radioFemale);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
passDate();
}
});
}
public void passDate(){
Intent intent = new Intent(this,SecondActivity.class);
//将数据存入Intent对象
intent.putExtra("name", et_name.getText().toString().trim());
intent.putExtra("password", et_password.getText().toString().trim());
String str = "";
if(raidoMale.isChecked()){
str = "男";
}else if (radioFemale.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.main, menu);
return true;
}
}
activity_main.xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="密码:"
android:width="80dp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码"
android:textSize="14dp"
android:inputType="textPassword"/>
LinearLayout>
<LinearLayout
android:id="@+id/regist_username"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="用户名:"
android:width="80dp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的用户名"
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"
android:layout_centerHorizontal="true">
<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:checked="true"
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>
2.创建一个活动用来显示得到的数据
SecondActivity.class
package com.example.passdata;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextClock;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView tv_name,tv_password,tv_sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
//获取Intent对象
Intent intent = getIntent();
//取出key对应的value值
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("用户名:"+name);
tv_password.setText("密码:"+password);
tv_sex.setText("性别:"+sex);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity02, menu);
return true;
}
}
SecondActivity.class的界面activity02.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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.在AndroidMainfest.xml 更改下活动标题栏
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.passdata"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.passdata.MainActivity"
android:label="填写用户信息" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<activity
android:name="com.example.passdata.SecondActivity"
android:label="展示用户信息" >
activity>
application>
manifest>