码迷,mamicode.com
首页 > 移动开发 > 详细

android_Intent对象初步(Activity传统的价值观念)

时间:2015-09-21 09:14:55      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

说明:初步Intent物。主要使用Intent对象在Activity之间传递数据的方法。

样例:由MainActivity→OtherActivity的跳转过程中,把数据传递给OtherActivity并显示出来。

在讲步骤之前,先来看看Intent到底是个什么东西先。

Intent对象的基本概念:

1、Intent对象是Android应用程序组件之中的一个;

2、Intent对象在Android系统其中表示一种意图;

3、Intent其中最重要的内容是action和data。

(还有Component name、Category、Extras、Flags。)

步骤:1.在MainActivity里面生成一个Intent对象,在Intent对象里面使用putExtra()系列方法,把数据放到Intent对象其中去。

activity_main.xml里面放置一个Button,点击跳转到OtherActivity。

<Button
        android:id="@+id/btnId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动第二个Activity" />
然后生成Intent,使用putExtra()向Intent对象其中存储数据。
package com.away.b_04_intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button=(Button)findViewById(R.id.btnId);
        button.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, OtherActivity.class);
			
			intent.putExtra("com.away.b_04_intent.Age", 21);
			intent.putExtra("com.away.b_04_intent.Name", "Chay");
			startActivity(intent);
            //startActivity(new Intent(MainActivity.this, OtherActivity.class).putExtra("name","Chay").put....);
		}
    }
}
2.在OtherActivity里面使用getXXXExtra()系列方法从Intent对象其中取出数据。

package com.away.b_04_intent;

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

public class OtherActivity extends Activity {
	private TextView textView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);

		Intent intent = getIntent();
		int age = intent.getIntExtra("com.away.b_04_intent.Age", 10);
		String name = intent.getStringExtra("com.away.b_04_intent.Name");

		textView = (TextView) findViewById(R.id.textView);
		textView.setText(name + ":" + age + "");
	}
}

效果图:

技术分享

上面是传递的数据是比較少的,假设数据比較多。就须要使用Bundle类了,代码例如以下:

MainActivity:

<pre name="code" class="java">Intent intent = new Intent(MainActivity.this, OtherActivity.class);  

/* 通过Bundle对象存储须要传递的数据 */  
Bundle bundle = new Bundle();  
/*字符、字符串、布尔、字节数组、浮点数等等。都能够传*/  
bundle.putString("Name", "Away");  
bundle.putBoolean("Ismale", true);  
  
/*把bundle对象assign给Intent*/  
intent.putExtras(bundle);  
//intent.putExtra("bundle", bundle);

startActivity(intent);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>

OtherActivity:
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    /*载入页面*/  
    setContentView(R.layout.other);  
      
    /*获取Intent中的Bundle对象*/  
    Bundle bundle = this.getIntent().getExtras();  
//  Bundle bundle = getIntent().getBundleExtra("bundle");
    /*获取Bundle中的数据,注意类型和key*/  
    String name = bundle.getString("Name");  
    boolean ismale = bundle.getBoolean("Ismale");  
}

-----------------------------------------------割-----------------------------------------------

ps:有时。在页面跳转之后,须要返回到之前的页面,同一时候要保留用户之前输入的信息。这个时候该怎么办呢?

在页面跳转后,前一个Activity已经被destroy了。假设要返回并显示数据。就必须将前一个Activity再次唤醒,同一时候调用某个方法来获取并显示数据。

要实现这个效果,须要做下面几步:

1. 首先。从A页面跳转到B页面时。不能够使用“startActivity()”方法。而要使用“startActivityForResult()”方法。

2. 在A页面的Activity中,须要重写“onActivityResult()”方法

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
  switch(requestCode){
      case RESULT_OK:
      /*取得来自B页面的数据,并显示到画面*/
      Bundle bundle = data.getExtras();

      /*获取Bundle中的数据,注意类型和key*/
      String name = bundle.getString("Name");
      boolean ismale = bundle.getBoolean("Ismale");
  }
}
3. 在B页面上加一个返回button,并在事件写例如以下代码:

/*给上一个Activity返回结果*/
B.this.setResult(RESULT_OK,intent);
/*结束本Activity*/
B.this.finish(); 

比如。选择日期,然后返回。

欢迎交流 http://blog.csdn.net/ycwol/article/details/39859341

版权声明:本文博主原创文章,博客,未经同意不得转载。

android_Intent对象初步(Activity传统的价值观念)

标签:

原文地址:http://www.cnblogs.com/hrhguanli/p/4825178.html

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