Android初试--Intent传递数据

android中的Intent【意图】[请求]
Intent【意图】[请求]的作用
1.Intent可以激活组件【android核心组件】
2.Intent传递数据
   通过Intent传递数据的方式有2中
    1.是将需要传递的数据封装到Intent对象中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="输入数据的Activity"
        android:textSize="30sp" />
    <EditText
        android:id="@+id/input_id_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:text="请输入学号" >
        <requestFocus />
    </EditText>
    <EditText
        android:id="@+id/input_name_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="请输入姓名" />
    <EditText
        android:id="@+id/input_age_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:text="请输入年龄" />
    <EditText
        android:id="@+id/input_address_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="请输入地址" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传递数据给ShowActivity" 
        android:onClick="openShowActivity"
        />
</LinearLayout>

package com.click369.intentdemo1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    private  EditText  input_id=null;
    private  EditText  input_name=null;
    private  EditText  input_age=null;
    private  EditText  input_address=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }
    /**
     * 初始化文本框
     */
private void initview() {
input_id=(EditText)this.findViewById(R.id.input_id_editText);
input_name=(EditText)this.findViewById(R.id.input_name_editText);
input_age=(EditText)this.findViewById(R.id.input_age_editText);
input_address=(EditText)this.findViewById(R.id.input_address_editText);
}
/**
*按钮的单击事件
* @param view
*/
public  void  openShowActivity(View  view){
//得到文本框中输入的被传递的数据
int  id=Integer.parseInt(input_id.getText().toString());
String  name=input_name.getText().toString();
int  age=Integer.parseInt(input_age.getText().toString());
String  address=input_address.getText().toString();
//创建一个Intent对象
Intent  intent=new Intent();
//将得到的数据封装到Intent对象中
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("age", age);
intent.putExtra("address", address);
//显示意图
intent.setClass(this, ShowActivity.class);
startActivity(intent);

}

}


package com.click369.intentdemo1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ShowActivity extends Activity{
    private  TextView  show_data_valaue=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_activity_layout);
show_data_valaue=(TextView)this.findViewById(R.id.show_textView);
//接收来自MainActivity的意图对象
Intent  intent=this.getIntent();
//从意图对象中得到需要显示的数据
int  id=intent.getIntExtra("id", 0);
String name=intent.getStringExtra("name");
int  age=intent.getIntExtra("age", 0);
String address=intent.getStringExtra("address");
//将从intent对象中得到的数据组织成String
String  infomsg="编号:"+id+"\n姓名:"+name+"\n年龄:"+age+"\n地址:"+address;
//将组织好的数据显示在TextView上
show_data_valaue.setText(infomsg);
}
}

以上的操作都是传递的是基本数据类型的数据.

传递对象型的数据


package com.click369.intentdemo1;
import java.io.Serializable;

public class Student implements Serializable{
   private  int  id;
   private  String  name;
   private  int  age;
   private  String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
   
}
必须实现序列化接口。

public  void  openShowActivity(View  view){
//得到文本框中输入的被传递的数据
int  id=Integer.parseInt(input_id.getText().toString());
String  name=input_name.getText().toString();
int  age=Integer.parseInt(input_age.getText().toString());
String  address=input_address.getText().toString();
//将得到得数据封装到对应的对象中
Student  student=new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
Intent  intent=new Intent();
//将得到的对象封装到意图对象中
intent.putExtra("student", student);
intent.setClass(this, ShowActivity.class);
startActivity(intent);

}

intent.putExtra("student", 序列化接口的子类);


             //接收来自MainActivity的意图对象
Intent  intent=this.getIntent();
Student student=(Student)intent.getSerializableExtra("student");
//将从intent对象中得到的数据组织成String
String  infomsg="编号:"+student.getId()+"\n姓名:"+student.getName()+
"\n年龄:"+student.getAge()+"\n地址:"+student.getAddress();
//将组织好的数据显示在TextView上
show_data_valaue.setText(infomsg);

    2.是将需要传递的数据封装Bundle对象,然后将Bundle对象装入Intent对象中。

package comclick369.intentdemo2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
//定义需要的控件
private EditText  input_id=null;
private EditText  input_name=null;
private EditText  input_age=null;
private EditText  input_address=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }
    /**
     * 初始化控件
     */
private void initview() {
input_id=(EditText)this.findViewById(R.id.input_id);
input_name=(EditText)this.findViewById(R.id.input_name);
input_age=(EditText)this.findViewById(R.id.input_age);
input_address=(EditText)this.findViewById(R.id.input_address);
}
    
/**
* 按钮点击事件
* @param view
*/
public  void  openOtherActivity(View  view){
// //得到文本框中的数据
// int  stuid=Integer.parseInt(input_id.getText().toString()); 
// String stuname= input_name.getText().toString();
// int  stuage=Integer.parseInt(input_age.getText().toString());
// String  stuaddress=input_address.getText().toString();
// //将得到数据封装到Bundle对象
// //创建Bundle对象
// Bundle  bundle=new Bundle();
// bundle.putInt("id", stuid);
// bundle.putString("name", stuname);
// bundle.putInt("age", stuage);
// bundle.putString("address", stuaddress);
//
// //将Bundle对象封装到Intent对象中
// Intent  intent=new Intent();
// intent.putExtra("mybundle", bundle);
// intent.setClass(this, OtherActivity.class);
// startActivity(intent);

//得到文本框中的数据
int  stuid=Integer.parseInt(input_id.getText().toString()); 
String stuname= input_name.getText().toString();
int  stuage=Integer.parseInt(input_age.getText().toString());
String  stuaddress=input_address.getText().toString();
//将得到的数据组织成一个对象
Student  student=new Student();
student.setStuid(stuid);
student.setStuname(stuname);
   student.setStuage(stuage);
   student.setStuaddress(stuaddress);
   
   Bundle  bundle=new Bundle();
//bundle.putSerializable(key, value)可以传递一个对象型的数据
//但是该对象的类必须要实现序列化接口。
   bundle.putSerializable("student", student);
//将Bundle对象封装到Intent对象中
Intent  intent=new Intent();
intent.putExtra("mybundle", bundle);
intent.setClass(this, OtherActivity.class);
startActivity(intent);
}
    
}


package comclick369.intentdemo2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends  Activity{
   private  TextView  show_data_textview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.other_activity_layout);
show_data_textview=(TextView)this.findViewById(R.id.show_data_testview);
// //得到来自MainActivity的Intent对象
// Intent  intent=this.getIntent();
// //得到封装数据用的bundle对象
// Bundle bundle=intent.getBundleExtra("mybundle");
//   //从bundle中的到需要显示的数据
// int stuid=bundle.getInt("id");
// String  stuname=bundle.getString("name");
// int  stuage=bundle.getInt("age");
// String  stuaddress=bundle.getString("address");
// //将得到的数据组织成字符串
// String  info="id="+stuid+"\n"+
//             "name="+stuname+"\n"+
//     "age="+stuage+"\n"+
//             "address="+stuaddress;
//
// show_data_textview.setText(info);  

//得到来自MainActivity的Intent对象
Intent  intent=this.getIntent();
//得到封装数据用的bundle对象
Bundle bundle=intent.getBundleExtra("mybundle");
//从bundle中得到Student对象
Student  student=(Student)bundle.getSerializable("student");
//将得到的数据组织成字符串
String  info="id="+student.getStuid()+"\n"+
            "name="+student.getStuname()+"\n"+
    "age="+student.getStuage()+"\n"+
            "address="+student.getStuaddress();
show_data_textview.setText(info);  
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值