天天看點

android元件通訊 Intent-Category屬性

程式檔案

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

代碼  

package com.amaker.test;  

import android.app.Activity;  

import android.content.Intent;  

import android.os.Bundle;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

/**  

 * 測試Intent 的 Category屬性  

 */  

public class MainActivity extends Activity {  

    // 聲明 Button  

    private Button b1;  

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        // 設定目前布局  

        setContentView(R.layout.main);  

        // 執行個體化 Button  

        b1 = (Button)findViewById(R.id.Button01);  

        // 為Button 添加監聽器  

        b1.setOnClickListener(new OnClickListener() {  

            @Override  

            public void onClick(View v) {  

                // 執行個體化Intent  

                Intent i = new Intent();  

                // 添加Action屬性  

                i.setAction(Intent.ACTION_MAIN);  

                // 添加Category屬性  

                i.addCategory(Intent.CATEGORY_HOME);  

                // 啟動Activity  

                startActivity(i);  

            }  

        });  

    }  

布局檔案

/Chapter06_Intent_Category/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" android:layout_width="fill_parent" 

    android:layout_height="fill_parent">  

    <TextView   

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content"   

    android:text="Intent Category 測試" />  

    <Button   

    android:id="@+id/Button01"   

    android:layout_width="wrap_content" 

    android:text="回到Home"></Button>  

</LinearLayout> 

本文轉自linzheng 51CTO部落格,原文連結:http://blog.51cto.com/linzheng/1080666

繼續閱讀