码迷,mamicode.com
首页 > 其他好文 > 详细

Intent传递数据

时间:2014-11-26 16:23:01      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   os   使用   

方式比较多,先看看代码,一会儿再总结。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="向第二个活动传递数据" />
</RelativeLayout>

secondactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<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/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是第二个活动" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回数据" />

</LinearLayout>

SecondActivity.java

public class SecondActivity extends Activity implements OnClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        
        Button btn = (Button) findViewById(R.id.btn2);
        btn.setOnClickListener(this);
        
        //第一种方式 intent.getStringExtra(String);或者intent.getIntExtra(String,int defaultValue);
        Toast.makeText(this, getIntent().getStringExtra("str1"), Toast.LENGTH_LONG).show();
        //Toast.makeText(this, getIntent().getIntExtra("age", 0), Toast.LENGTH_LONG).show();
        
        //第二种方式、利用Bundle对象,bundle.getString()或者bundle.getInt();
        //Bundle bundle = getIntent().getExtras();
        //Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_LONG).show();
        //Toast.makeText(this,bundle.getInt("age2"),Toast.LENGTH_LONG).show();
    }
    @Override
    public void onClick(View v) 
    {
        Intent intent = new Intent();
        //返回数据的
        //第一种方式 putExtra()
        intent.putExtra("age3", 45);
        
        //第二种方式setExtra()
        //intent.setData(Uri.parse("我是返回数据"));
        
        setResult(RESULT_OK,intent);  
        
        //一定记得关闭当前activity并返回控制权
        finish();
    }
}

 

在AndroidManifest.xml中注册。

<activity 
            android:name=".SecondActivity"
            android:label="@string/second_activity">
            <intent-filter >
                <action android:name="com.example.passingdata"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

 

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {
        //传递数据的话,也要带上请求码
        Intent intent = new Intent("com.example.passingdata.SecondActivity");
                
        //传递数据的方式1: 使用 intent.putExtra();
        intent.putExtra("str1", "String1");
        intent.putExtra("age", 25);
                
        //方式2:bundle.putString(); bundle.putInt();最后把bundle绑定到intent上intent.putExtras();
        //Bundle bundle = new Bundle();
        //bundle.putString("str2", "String2");
        //bundle.putInt("age2", 35);
        //intent.putExtras(bundle);  //从这里可以看出,bundle作用上也相当于一个集合
                
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if(requestCode==1)
        {
            if(resultCode==RESULT_OK)
            {
                //取得数据
                //方式1: getIntExtra()
                
                Toast.makeText(this, 
                        Integer.toString(data.getIntExtra("age3", 0)), 
                        Toast.LENGTH_LONG).show();
                
                //方式2: intent.getData();  它对应了Uri.parse()那种
                //Toast.makeText(this, data.getData().toString(), Toast.LENGTH_LONG).show();
            }
                
        }
    }
}

(注意不要在传递数据的时候,用多种方式,很容易出错)

看看效果:

bubuko.com,布布扣

点击一个“返回数据”

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣


闭着眼小结一下:(传递数据死活离不开intent,目前我了解到的是这样)

 

发送方:

    a.  intent.putExtra() ,也就是putExtra()的方式添加 Key/Value

    b. 先把键值对给bundle对象,bundle.putString()或者bundle.putInt();然后把bundle给intent:intent.putExtras();

    c.intent.setData(Uri.parse(String))   //应该是用于打开网页吧?

--------------(发送完了记得finish())

 

接受方:

    a.  getIntent()之后 ,用intent.getStringExtra() /  intent.getIntExtra();

    b. getIntent()之后,intent.getExtras()获取bundle对象,在之后,bundle.getString() /  bundle.getInt()

    c.intent.getData().toString();

 

好了,就这么多,记牢。

Intent传递数据

标签:android   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/bluechip/p/4123359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!