前言:在信息时代,人们不必像古代人那样飞鸽传书,随便一个短信、微信、QQ、微博都可以和自己亲爱的小伙伴快速沟通交流。在这样眼花缭乱的信息时代,选择一种合适自己的沟通工具是很有必要的,Android中的Activity与Activity之间的传参方法也是很多种的,在项目中怎么选择信息交互方法,就看项目的需求和自己的经验。
弄例子之前需要了解一个东西:Intent;
书上和网上看到的概念解释都很多,但本人喜欢它的英文意思:意图。你的意图想干嘛,跟它说了就行。比如我要传参,那你在这个Intent中设置完了,到第二个界面通过你设置的意图传参,就可以解析出来。比如你意图要打开界面,你在Intent里面设置好要打开的界面参数,然后通过方法调用就可以。或者更简单的说明就是起传递作用!
开始实战理解:
1.新建个项目:ActivityIntent
2.根据上一篇,创另外一个界面,创建的文件为:activity_first.xml和FirstActivity
activity_first.xml中界面布局的代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/close_1"
android:layout_below="@id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭"
/>
</RelativeLayout>
<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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="界面之间传参"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/Open_1"
android:layout_below="@id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Intent传参(第一种)"
/>
<Button
android:id="@+id/Open_2"
android:layout_below="@id/Open_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Intent传参(第二种)"
/>
<Button
android:id="@+id/Open_3"
android:layout_below="@id/Open_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Intent传参(第三种)"
/>
</RelativeLayout>
3.把新建的界面布局加入程序,修改下AndroidManifest.xml,在</application>节点前添加内容如下::
<activity
android:name=".FirstActivity"
android:label="@string/app_name" >
</activity>
4.编写入口代码MainActivity如下:
package com.wyz.activityintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button intentBtn = (Button)findViewById(R.id.Open_1);
Button intentBtn_2 = (Button)findViewById(R.id.Open_2);
Button intentBtn_3 = (Button)findViewById(R.id.Open_3);
intentBtn.setOnClickListener(new BtnListener());
intentBtn_2.setOnClickListener(new BtnListener());
intentBtn_3.setOnClickListener(new BtnListener());
}
class BtnListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
switch(v.getId())
{
case R.id.Open_1: //第一种
intent.putExtra("name", "wyz_1_");
intent.putExtra("age", 18);
startActivity(intent);
break;
case R.id.Open_2: //第二种
Bundle bundle = new Bundle();
bundle.putString("name", "wyz_2_");
bundle.putInt("age", 27);
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.Open_3: //第三种
intent.putExtra("name", "wyz_3_");
intent.putExtra("age", 35);
startActivityForResult(intent, 0);
break;
default:
break;
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0) //这个请求码对应startActivityForResult(intent, 0);
{
if(resultCode == 1) //这个请求码对应打开界面的setResult(1, resultIntent);
{
String sRet = data.getStringExtra("return");
Toast.makeText(getApplicationContext(), sRet, Toast.LENGTH_LONG).show();;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
4.编写另外一个界面业务代码FirstActivity如下:
package com.wyz.activityintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FirstActivity extends Activity {
int iType=0; //判断是第几种方法触发,默认0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
EditText text = (EditText)findViewById(R.id.text);
Button close_btn = (Button)findViewById(R.id.close_1);
close_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(iType == 3) //等于第三种时,关闭界面返回参数
{
Intent resultIntent = new Intent();
resultIntent.putExtra("return", "Hello wyz");
setResult(1, resultIntent);
FirstActivity.this.finish();
}
else
{
FirstActivity.this.finish();
}
FirstActivity.this.finish();
}
});
Intent intent = getIntent();
String sName = intent.getStringExtra("name");
//int iAge = intent.getIntExtra("age", 0); //获取参数第一种
int iAge = intent.getExtras().getInt("age"); //获取参数第二种
String sAge = String.valueOf(iAge);
text.setText(sName+sAge); //设置显示结果
if(sName.compareTo("wyz_3_") == 0)
{
iType = 3;
}
}
}
实战效果:
注:
1.简单理解,第一种主要是用intent直接把参数放入意图中;第二种通过bundle打包参数后放入意图中;第三种就是通过startActivityForResult让意图打开可以返回参数的界面。
2.从资料了解,还有隐式打开界面方法,即在AndroidManifest.xml配置清单中配置好intent-filter意图过滤,说俗点就是给个暗号;然后再程序中给这个暗号,在多个界面时,就会根据这个暗号在AndroidManifest.xml配置中找到对应的启动界面。
a.修改下AndroidManifest.xml,在</application>节点前添加内容改为如下:
<activity
android:name=".FirstActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.wyz.open_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
b.MainActivity中启动界面代码修改如下:
intent.setAction("android.wyz.open_1");
//不加下面这行也行,因为intent的这个属性默认值即系Intent.CATEGORY_DEFAULT
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
c.暗号信息中很重要的就是action,intent-filter必须包含,名字可以随意自定义;还有一个很重要的就是
<category android:name="android.intent.category.DEFAULT"/>必须包含;如果进一步了解,还可以知道里面还可以配置data,这样通过分配不同参数,暗号就会传达不同信息,多个界面的时候就可以根据暗号打开哪个界面,获取到不同数据