程序文件

/Chapter06_Intent_Category/src/com/amaker/test/MainActivity.java

 
  
  1. 代码  
  2.  
  3. package com.amaker.test;  
  4.  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.  
  12. /**  
  13.  * 测试Intent 的 Category属性  
  14.  */  
  15. public class MainActivity extends Activity {  
  16.     // 声明 Button  
  17.     private Button b1;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         // 设置当前布局  
  22.         setContentView(R.layout.main);  
  23.         // 实例化 Button  
  24.         b1 = (Button)findViewById(R.id.Button01);  
  25.         // 为Button 添加监听器  
  26.         b1.setOnClickListener(new OnClickListener() {  
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 // 实例化Intent  
  30.                 Intent i = new Intent();  
  31.                 // 添加Action属性  
  32.                 i.setAction(Intent.ACTION_MAIN);  
  33.                 // 添加Category属性  
  34.                 i.addCategory(Intent.CATEGORY_HOME);  
  35.                 // 启动Activity  
  36.                 startActivity(i);  
  37.             }  
  38.         });  
  39.     }  

布局文件

/Chapter06_Intent_Category/res/layout/main.xml

 

 
  
  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  5.     android:orientation="vertical" android:layout_width="fill_parent" 
  6.     android:layout_height="fill_parent">  
  7.       
  8.     <TextView   
  9.     android:layout_width="fill_parent" 
  10.     android:layout_height="wrap_content"   
  11.     android:text="Intent Category 测试" />  
  12.       
  13.     <Button   
  14.     android:id="@+id/Button01"   
  15.     android:layout_width="wrap_content" 
  16.     android:layout_height="wrap_content"   
  17.     android:text="回到Home"></Button>  
  18.       
  19.       
  20. </LinearLayout>