说明:初探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里面使用get
XXXExtra()系列方法从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页面上加一个返回按钮,并在事件写如下代码:
/*给上一个Activity返回结果*/
B.this.setResult(RESULT_OK,intent);
/*结束本Activity*/
B.this.finish();
例如,选择日期,然后返回。。
欢迎交流 http://blog.csdn.net/ycwol/article/details/39859341