现在的位置: 首页 > 综合 > 正文

Android开发循序渐进实例2–画面间数据传递例子

2012年12月18日 ⁄ 综合 ⁄ 共 3114字 ⁄ 字号 评论关闭

在Android之中,画面间的数据传递主要是通过Intent的putExtra方法来实现的,传递的数据结构就是一个Map数据(Name/Value对,特别注意不同的是必须为基本数据类型),下面将通过实例的方式来讲述具体的实现方法。

本实例中主要讲述如何将画面一的数据传递到画面二当中,在画面二对画面一传递回来的数据进行一定的计算处理后然后将数据返回到画面一当中。

1. 按照如下的配置以及在开发循序渐进实例1中描述的方法创建整个项目Base(特别需要注意在AndroidManifest.xml中加入对SecondActivity的配置,具体方法见实例1):
 project Name: ExampleTwo
 Platform: Android2.0;
 Application name: ExampleOne
 package name: com.example
 Activity: FirstActivity, SecondActivity
 Resource file: first.xml, second.xml
 first.xml 有二个Button:
 Id: @+id/ForwardByView
 Text: Forward to second view by view
        Id: @+id/ForwardByStatus
 Text: Forward to second view by status
 second.xml 有一个Button: BackButton
 Id: @+id/Back
 Text: Back to first view

2. 在FirstView中加入如下的代码(对于重载onActivityResult可在该类上使用鼠标右键菜单并且选择Source->Override/Implement methods...在弹出框中选择onActivityResult()并且点击OK):
        private void forward_view_event_byview() {
  Button button = (Button) findViewById(R.id.ForwardView);
  button.setOnClickListener(forwardByviewbutton_listener);
 }
 
 private Button.OnClickListener forwardByviewbutton_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   Intent intent = new Intent();
   intent.setClass(FirstActivity.this, SecondActivity.class);
   intent.putExtra("Param", "action view");

   startActivityForResult(intent, 1);
  }
 };

 private void forward_view_event_bystatus() {
  Button button = (Button) findViewById(R.id.ForwardStatus);
  button.setOnClickListener(forwardBystatusbutton_listener);
 }

 private Button.OnClickListener forwardBystatusbutton_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   Intent intent = new Intent();
   intent.setClass(FirstActivity.this, SecondActivity.class);
   intent.putExtra("Param", "action status");

   startActivityForResult(intent, 2);
  }
 };
 
 private void showReturnValue(int requestCode, int resultCode, Intent data) {
  Bundle extras = data.getExtras();
  if (extras != null) {
   String returnValue = extras.getString("ReturnValue");
   setTitle(returnValue + " ["+ String.valueOf(requestCode) + "-" + String.valueOf(resultCode) + "]");
  }
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  showReturnValue(requestCode, resultCode, data);
 }

   最后在onCreate()结束处插入如下代码调用:
    forward_view_event_byview();
    forward_view_event_bystatus();

3. 在SecondView中加入如下代码:
 private void show_input_values() {
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   String inputValue = extras.getString("Param");
   setTitle(inputValue);
  }
 }

 private void back_view_event() {
  Button button = (Button) findViewById(R.id.Back);
  button.setOnClickListener(back_listener);
 }
 
 private Button.OnClickListener back_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   Bundle retBundle = new Bundle();
   retBundle.putString("ReturnValue", "String comes from the second view");
   Intent intent = new Intent();
   intent.putExtras(retBundle);
   setResult(RESULT_OK, intent);
   finish();
  }
 };

并且在onCreate()函数的末尾处插入如下的代码调用:
 show_input_values();
 back_view_event();

总结:本实例展示了如何将FirstView中名为"Param"的参数值传入到SecondView中,并且展示了2种RequestCode下传送的方法,在SecondView中展示了如何将FirstView传送的值进行显示(基本处理),然后在返回时如何将名为"ReturnValue"的参数值返回到FirstView中,并且将其显示出来。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jackxinxu2100/archive/2010/01/26/5258330.aspx

抱歉!评论已关闭.