Android学习笔记--Activity中使用Intent传值示例代码


Intent,又称为意图,是一种运行时绑定机制,它能在程序运行的过程中链接两个不同的组件(Activity、Service、BroadcastReceiver)。通过Intent,程序可以向Android表达某种请求或意愿,Android会根据意愿的内容选择适当的组件来请求。

在这些组件之间的通讯中,主要是由Intent协助完成的。Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
通过Intent请求Activity,必须在AndroidManifest.xml文件中对被请求的Activity新增标签配置,否则会导致错误。

Intent一般包含两个主要信息,action、data。
action:表示这个Intent此次操作的动作。
data:表示这次动作涉及的数据。

通过一个例子来展示Activity中使用Intent导向新Activity并传递数据。此程序仅在两个页面之间相互跳转,但是每次跳转会创建新的Activity,所以在startActivity()之后需要调用finish()销毁当前Activity,如果不销毁,多次跳转后,程序的Activity栈中会存放多个Activity,点击设备的返回按钮,会发现会一直向后退。

主要步骤:
新建Android项目,增加新布局文件other.xml,新增Activity类otherActivity.class,用于接受Intent并展示other.xml。
在MainActivity类中,声明一个Intent类,通过Intent的构造函数指明源和目标。
获得Intent后,使用Intent.putExtra()方法对其传入数据。
调用Activity.startActivity启动这个Intent。
在otherActivity类中,使用Activity.getIntent()获得当前Activity的Intent。
获得Intent后,使用Intent.getXxxExtra()方法获得其中保存的数据。
在AndroidManifest.xml配置otherActivity节点。

示例代码
步骤2--3:
复制代码 代码如下:

public class MainActivity extends Activity {
private TextView textView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent构造函数:Intent来源;Intent目的。
Intent intent =new Intent(MainActivity.this,otherActivity.class);
intent.putExtra("data", "当前是页面2,信息来自页面1");
startActivity(intent);//启动Activity
finish();
}
});
}
}

步骤4--5:
复制代码 代码如下:

public class otherActivity extends Activity {
private Button btn;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView=(TextView)findViewById(R.id.textView2);
btn=(Button)findViewById(R.id.button2);
//通过Activity.getIntent()获取当前页面接收到的Intent。
Intent intent =getIntent();
//getXxxExtra方法获取Intent传递过来的数据
String msg=intent.getStringExtra("data");
textView.setText(msg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(otherActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}

步骤7:
复制代码 代码如下:

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.bgxt.IntentForAc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".otherActivity"/>
</application>

从Activity中返回数据
上面例子中只是介绍了Activity通过Intent传递数据,然而在实际应用中,不仅仅需要向Activity传递数据,而且要从Activity中返回数据,虽然返回数据和传递数据类似,但是还是有部分区别。
主要区别如下:
传递数据需要使用Activity.startActivityForResult()方法启动Activity,需要传递请求码,而不是Activity.startActivity()。
返回数据的时候,调用Activity.setResult()方法设置返回Intent以及返回码。
需要重写源Activity的onActivityResult()方法以便于接受返回的Intent,在onActivityResult()中会判断请求码和响应码。
通过一个例子说明从Activity返回数据。此程序有两个Activity,在MainActivity中输入加法运算的计算数,跳转到otherActivity中输入计算结果,并在点击返回后,把计算结果输出到MainActivity中。
示例代码
MainActivity:
复制代码 代码如下:

public class MainActivity extends Activity {
private EditText one,two,result;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one=(EditText)findViewById(R.id.one);
two=(EditText)findViewById(R.id.two);
result=(EditText)findViewById(R.id.result);
btn=(Button)findViewById(R.id.btnGo);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int ione=Integer.parseInt(one.getText().toString());
int itwo=Integer.parseInt(two.getText().toString());
Intent intent=new Intent(MainActivity.this, otherActivity.class);
intent.putExtra("one", ione);
intent.putExtra("two", itwo);
//启动需要监听返回值的Activity,并设置请求码:requestCode
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//当otherActivity中返回数据的时候,会响应此方法
//requestCode和resultCode必须与请求startActivityForResult()和返回setResult()的时候传入的值一致。
if(requestCode==1&&resultCode==2)
{
int three=data.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
@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;
}
}

otherActivity:
复制代码 代码如下:

public class otherActivity extends Activity {
TextView tvShow;
EditText etResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
tvShow=(TextView)findViewById(R.id.tvShow);
etResult=(EditText)findViewById(R.id.etResult);
Intent intent=getIntent();
int a=intent.getIntExtra("one", 0);
int b=intent.getIntExtra("two", 0);
tvShow.setText(a+" + "+b+" = "+" ? ");
Button btnResult=(Button)findViewById(R.id.btnReturn);
btnResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//新声明一个Intent用于存放放回的数据
Intent i=new Intent();
int result=Integer.parseInt(etResult.getText().toString());
i.putExtra("three", result);
setResult(2, i);//设置resultCode,onActivityResult()中能获取到
finish();//使用完成后结束当前Activity的生命周期
}
});
}
}

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3