Chinaunix首页 | 论坛 | 博客
  • 博客访问: 868782
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2011-01-07 09:23:32

工程

MainActivity

Java代码

  1. package org.wp.activity;
  2.  
  3. /**
  4.  *
  5.  * 请求码的作用
  6.  * 使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity
  7.  * 我们需要为startActivityForResult()方法传入一个请求码(第二个参数)
  8.  * 请求码的值是根据业务需要由自已设定,用于标识请求来源
  9.  * 例如:一个Activity有两个按钮,点击这两个按钮都会打开同一个Activity
  10.  * 不管是那个按钮打开新Activity,当这个新Activity关闭后
  11.  * 系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法
  12.  * 在onActivityResult()方法
  13.  * 如果需要知道新Activity是由那个按钮打开的,并且要做出相应的业务处理
  14.  * 这时可以这样做
  15.  * switch(requestCode){
  16.  * case 1:
  17.  * // 来自按钮1的请求,作相应业务处理
  18.  * case 2:
  19.  * // 来自按钮2的请求,作相应业务处理
  20.  * }
  21.  *
  22.  *
  23.  * 结果码的作用
  24.  * 在一个Activity中,可能会使用startActivityForResult()方法打开多个不同的Activity处理不同的业务
  25.  * 当这些新Activity关闭后
  26.  * 系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法
  27.  * 为了知道返回的数据来自于哪个新Activity
  28.  * 在onActivityResult()方法中可以这样做(ResultActivity和NewActivity为要打开的新Activity):
  29.  * switch(resultCode){
  30.  * case 1:
  31.  * // ResultActivity的返回数据
  32.  * case 2:
  33.  * // NewActivity的返回数据
  34.  * }
  35.  *
  36.  */
  37.  
  38. import android.app.Activity;
  39. import android.content.ComponentName;
  40. import android.content.Intent;
  41. import android.os.Bundle;
  42. import android.util.Log;
  43. import android.view.View;
  44. import android.widget.Button;
  45.  
  46. public class MainActivity extends Activity {
  47.     private static final String TAG = "MainActivity";
  48.  
  49.     @Override
  50.     public void onCreate(Bundle savedInstanceState) {
  51.         super.onCreate(savedInstanceState);
  52.         setContentView(R.layout.main);
  53.  
  54.         Button button = (Button) this.findViewById(R.id.button);
  55.         // 点击该按钮会打开一个新的Activity
  56.         button.setOnClickListener(new View.OnClickListener() {
  57.  
  58.             @Override
  59.             public void onClick(View arg0) {
  60.                 /*
  61.                  * 打开新的Activity
  62.                  * 在一个Activity中可以使用系统提供的
  63.                  * startActivity(Intent intent)方法打开新的Activity
  64.                  * 在打开新的Activity前
  65.                  * 你可以决定是否为新的Activity传递参数
  66.                  */
  67.  
  68.                 /*
  69.                  * 第一种写法
  70.                  * 新建一个显式意图
  71.                  * 第一个参数为当前Activity类对象
  72.                  * 第二个参数为你要打开的Activity类
  73.                  *
  74.                  * 显示意图
  75.                  * 通过调用setClass()或setComponent方法设置组件的意图为显示意图
  76.                  */
  77.                 Intent intent = new Intent(MainActivity.this,
  78.                         OtherActivity.class);
  79.  
  80.                 // 第二种写法
  81.                 // Intent intent = new Intent();
  82.                 // intent.setClass(MainActivity.this, OtherActivity.class);
  83.  
  84.                 // 第三种写法
  85.                 // Intent intent = new Intent();
  86.                 // intent.setComponent(new ComponentName(MainActivity.this,
  87.                 // OtherActivity.class));
  88.  
  89.                 /**
  90.                  * Intent提供了各种常用类型重载后的putExtra()方法
  91.                  * 如:putExtra(String name, String value)
  92.                  * putExtra(String name, long value)
  93.                  * 在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象
  94.                  * 如果不存在就会新建Bundle对象
  95.                  * 以后调用putExtra()方法传入的值都会存放于该Bundle对象
  96.                  */
  97.  
  98.                 // 打开新的Activity,并传递若干个参数给它
  99.                 intent.putExtra("id", 10);
  100.                 intent.putExtra("name", "张三");
  101.  
  102.                 /**
  103.                  * Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值
  104.                  * 相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法
  105.                  * 如:putString()/getString()和putInt()/getInt()
  106.                  * putXxx()用于往Bundle对象放入数据
  107.                  * getXxx()方法用于从Bundle对象里获取数据
  108.                  * Bundle的内部实际上是使用了HashMap类型的变量来存放putXxx()方法放入的值
  109.                  *
  110.                  * 在调用Bundle对象的getXxx()方法时,方法内部会从该变量中获取数据
  111.                  * 然后对数据进行类型转换,转换成什么类型由方法的Xxx决定
  112.                  * getXxx()方法会把转换后的值返回
  113.                  */
  114.  
  115.                 // 该类用作携带数据
  116.                 Bundle bundle = new Bundle();
  117.                 bundle.putInt("id", 12);
  118.                 bundle.putString("name", "张一");
  119.                 // 为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换
  120.                 intent.putExtras(bundle);// 附带上额外的数据
  121.  
  122.                 // MainActivity.this.startActivity(intent);
  123.  
  124.                 /**
  125.                  * 如果你想在Activity中得到新打开Activity关闭后返回的数据
  126.                  * 你需要使用系统提供的
  127.                  * startActivityForResult(Intent intent, int requestCode)方法打开新的Activity
  128.                  * 新的Activity关闭后会向前面的Activity传回数据
  129.                  * 为了得到传回的数据 你必须在前面的Activity中重写
  130.                  * onActivityResult(int requestCode, int resultCode, Intent data)方法
  131.                  */
  132.                 // 为了获得返回结果
  133.                 // 第二个参数为请求码,可以根据业务需求自己编号
  134.                 MainActivity.this.startActivityForResult(intent, 2);
  135.  
  136.             }
  137.         });
  138.     }
  139.  
  140.     /**
  141.      * 当新Activity关闭后,新Activity返回的数据通过Intent进行传递
  142.      * Android平台会调用前面Activity
  143.      * 的onActivityResult()方法
  144.      * 把存放了返回数据的Intent作为第三个输入参数传入
  145.      * 在onActivityResult()方法中使用第三个输入参数可以取出新Activity返回的数据
  146.      */
  147.  
  148.     // 第一个参数为请求码,即调用startActivityForResult()传递过去的值
  149.     // 第二个参数为结果码,结果码用于标识返回数据来自哪个新Activity
  150.     @Override
  151.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  152.         Log.i(TAG, "requestCode=" + requestCode + ",resultCode=" + resultCode);
  153.         if (resultCode == 23) {
  154.             // 得到新Activity 关闭后返回的数据
  155.             Log.i(TAG, "result=" + data.getStringExtra("result"));
  156.         }
  157.         super.onActivityResult(requestCode, resultCode, data);
  158.     }
  159. }

OtherActivity

Java代码

  1. package org.wp.activity;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9.  
  10. public class OtherActivity extends Activity {
  11.     private TextView resultView;
  12.     private Button button;
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         // 这里可以使用setContentView(R.layout.otherui)显示某个视图.
  18.         setContentView(R.layout.otherui);
  19.  
  20.         Intent intent = this.getIntent();
  21.         int id = intent.getIntExtra("id", 0);
  22.         String name = intent.getStringExtra("name");
  23.         resultView = (TextView) this.findViewById(R.id.result);
  24.         resultView.setText("id为" + id + ",姓名为" + name);
  25.  
  26.         // 在新的Activity中接收前面Activity传递过来的参数:
  27.         // Bundle bundle = this.getIntent().getExtras();
  28.         // int idValue = bundle.getInt("id");
  29.         // String nameValue = bundle.getString("name");
  30.  
  31.         button = (Button) this.findViewById(R.id.finish);
  32.         button.setOnClickListener(new View.OnClickListener() {
  33.             @Override
  34.             public void onClick(View arg0) {
  35.                 // 数据是使用Intent返回
  36.                 Intent intent = new Intent();
  37.                 // 把返回数据存入Intent
  38.                 intent.putExtra("result", "DeathNote");
  39.                 /**
  40.                  * 使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity
  41.                  * 新Activity关闭前需要向前面的Activity返回数据
  42.                  * 需要使用系统提供的setResult(int resultCode, Intent data)方法实现
  43.                  *
  44.                  * setResult()方法的第一个参数值可以根据业务需要自己定义
  45.                  * RESULT_OK 是系统Activity类定义的一个常量,值为-1
  46.                  * public static final int RESULT_CANCELED = 0;
  47.                  * public static final int RESULT_OK = -1;
  48.                  * public static final int RESULT_FIRST_USER = 1;
  49.                  *
  50.                  */
  51.                 // 设置返回数据
  52.                 OtherActivity.this.setResult(23, intent);
  53.                 // 关闭Activity
  54.                 OtherActivity.this.finish();
  55.             }
  56.         });
  57.     }
  58. }

main.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <Button
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="@string/button"
  11.         android:id="@+id/button"
  12.     />
  13. </LinearLayout>

otherui.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="wrap_content"
  6.     >
  7.     <TextView
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="@string/content"
  11.     />
  12.     <TextView
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content"
  15.         android:id="@+id/result"
  16.     />
  17.     <Button
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:text="@string/finish"
  21.         android:id="@+id/finish"
  22.     />
  23. </LinearLayout>

strings.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, MainActivity!</string>
  4.     <string name="app_name">多个Activity</string>
  5.     <string name="content">这是新的Activity</string>
  6.     <string name="button">打开新的Activity</string>
  7.     <string name="finish">关闭Activity</string>
  8. </resources>

AndroidManifest.xml

Xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="org.wp.activity" android:versionCode="1" android:versionName="1.0">
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  5.         <activity android:name=".MainActivity" android:label="@string/app_name">
  6.             <intent-filter>
  7.                 <action android:name="android.intent.action.MAIN" />
  8.                 <category android:name="android.intent.category.LAUNCHER" />
  9.             </intent-filter>
  10.         </activity>
  11.         <!--
  12.             android:name 属性值的前面加了一个点
  13.             表示OtherActivity是当前包org.wp.activity下的类
  14.             如果类在应用的当前包下,可以省略点符号
  15.             如果类在应用的子包下必须加点
  16.             如:OtherActivity类在org.wp.activity.user包下
  17.             可以这样写:<activity android:name=".user.OtherActivity" />
  18.             android:label为activity的页面标题
  19.         -->
  20.         <activity android:name=".OtherActivity" android:label="@string/content" />
  21.     </application>
  22.     <uses-sdk android:minSdkVersion="7" />
  23. </manifest>



阅读(2373) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~