在这篇随笔里将讲解Activity的传值和回传值。
一、通过startActivity来进行Activity的传值
①.通过setClass方法来指定我们要跳转的Activity
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
②.通过setAction方法来我们要完成的一个action操作
Intent? intent = new Intent();
intent.setAction("com.xiaoluo.android_intent.second");
通过这种方式可以来指定我们的Intent对象要完成某个操作,这个操作可以是启动一个Activity,我们可以在AndroidManifest.xml中在 元素下指定一个 对象,然后其子元素声明一个 元素,这样我们可以将这个action动作绑定到了这个Activity上,即Android操作系统会去找与intent对象中指定的action名字的 对象,然后执行相应的动作,例如:
? ? ? ? ? ? android:name="com.xiaoluo.android_intent.SecondActivity"
? ? ? ? ? ? android:label="SecondActivity">
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ?
这样我们的Intent对象,在启动时,就会找到名字为 com.xiaoluo.android_intent.second 的对象,来启动我们的SecondActivity。
我们来看看如何在Activity对象中进行值的传递,也是通过 Intent 对象的各种putExtra方法来进行传递:
MainActivity:
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.button);
? ? ? ? button.setOnClickListener(new ButtonListener());
? ? }
? ? class ButtonListener implements OnClickListener
? ? {
? ? ? ? @Override
? ? ? ? public void onClick(View v)
? ? ? ? {
//? ? ? ? ? ? Intent intent = new Intent();
//? ? ? ? ? ? intent.setClass(MainActivity.this, SecondActivity.class);
? ? ? ? ? ? Intent? intent = new Intent();
? ? ? ? ? ? intent.setAction("com.xiaoluo.android_intent.second");
? ? ? ? ? ?
? ? ? ? ? ? intent.putExtra("com.xiaoluo.android_intent.age", 20); // 第一个参数指定name,android规范是以包名+变量名来命名,后面是各种类型的数据类型
? ? ? ? ? ? intent.putExtra("com.xiaoluo.android_intent.name", "xiaoluo");
? ? ? ? ? ?
? ? ? ? ? ? Bundle bundle = new Bundle(); // Bundle的底层是一个HashMap? ? ? ? ? ? bundle.putString("hello", "world");
? ? ? ? ? ? intent.putExtra("bundle", bundle);
? ? ? ? ? ?
? ? ? ? ? ? startActivity(intent);
? ? ? ? }
? ? }
? ?
? ? @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;
? ? }
}
SecondActivity:
public class SecondActivity extends Activity
{
? ? private TextView textView;
? ? private final String TAG = "SecondActivity";
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState)
? ? {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.second);
? ? ? ?
? ? ? ? textView = (TextView)findViewById(R.id.textView);
? ? ? ?
? ? ? ? //? ? 得到跳转到该Activity的Intent对象
? ? ? ? Intent intent = getIntent();
? ? ? ?
? ? ? ? int age = intent.getIntExtra("com.xiaoluo.android_intent.age", 10);
? ? ? ? String name = intent.getStringExtra("com.xiaoluo.android_intent.name");
? ? ? ? Bundle bundle = intent.getBundleExtra("bundle");
? ? ? ? String world = bundle.getString("hello");
? ? ? ?
? ? ? ? Log.i(TAG, age + ", " + name + ", " + world);
? ? ? ?
? ? ? ? textView.setText(name + " : " + age + ", " + world);
? ? ? ?
? ? ? ? System.out.println(intent);
? ? }
}
在第二个Activity对象中,可以通过 getIntent() 方法来得到跳转到这个Activity的Intent对象,然后通过 Intent 对象的各种 getXXExtra 方法来得到我们的传过来的值。
AndroidManifest.xml文件如下:
? ? package="com.xiaoluo.android_intent"
? ? android:versionCode="1"
? ? android:versionName="1.0" >
? ? ? ? ? ? android:minSdkVersion="8"
? ? ? ? android:targetSdkVersion="18" />
? ? ? ? ? ? android:allowBackup="true"
? ? ? ? android:icon="@drawable/ic_launcher"
? ? ? ? android:l