Android 學習之顯式啟用與隱式啟用Activity

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   color   ar   os   

在res介面裡面有兩個布局檔案activity_main和acivity_two

activity_main裡面有如下四個按鈕

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.twoactivity.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我是第一個介面" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click"        android:text="跳轉到第二個介面" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click2"        android:text="啟用系統的一個應用" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click3"        android:text="隱式意圖跳轉到第二個介面" />         <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click4"        android:text="啟用簡訊的介面" /></LinearLayout>

acivity_two.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.twoactivity.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我是第二個介面" />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/abc_menu_dropdown_panel_holo_dark" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click"        android:text="跳轉到第二個介面" /></LinearLayout>

現在我們需要點擊main裡面的"跳轉到第二個頁面",讓程式顯示地跳轉到two頁面裡面

下面我們在src下面建立兩個類,分別繼承Activity類

MainActivity.java

package com.example.twoactivity;import android.annotation.SuppressLint;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    // 當使用者點擊按鈕的時候跳轉到第二個介面    public void click(View view) {        // 單擊按鈕啟用第二個介面,跳轉        // Intent 意圖        /*         * Intent intent = new Intent(); intent.setClassName(this,         * "com.example.twoactivity.OtherScreenActivity");// 設定這個意圖要啟用的組件         * startActivity(intent);         */        Intent intent = new Intent(this, OtherScreenActivity.class);        startActivity(intent);    }    public void click2(View view) {        // 單擊按鈕啟用第二個介面,跳轉        // Intent 意圖        Intent intent = new Intent();        intent.setClassName("com.android.gallery",                "com.android.camera.GalleryPicker");// 設定這個意圖要啟用的組件        startActivity(intent);    }    /**     * 採用隱式意圖啟用第三個介面     *      * */    @SuppressLint("NewApi")    public void click3(View view) {        // 單擊按鈕啟用第二個介面,跳轉        // Intent 意圖        Intent intent = new Intent();        intent.setAction("com.example.xxx");        intent.addCategory("android.intent.category.DEFAULT");        // 指定資料類型        // 如果只要類型沒有資料        // intent.setType("vnd.android.cursor.item/haha");        // 如果只要資料沒有類型        // intent.setData(Uri.parse("itheima:gagaga"));        // 如果有類型和資料都有        intent.setDataAndTypeAndNormalize(Uri.parse("itheima:gagaga"),                "vnd.android.cursor.item/haha");        startActivity(intent);        // 動作資料        // 打人,打醬油        // 泡茶,泡妞        // 泡綠茶,泡紅塵,泡烏龍        // addCategory附加資訊    }    /**     * 啟用簡訊的服務     *      * */    public void click4(View view) {        Intent intent = new Intent();        intent.setAction("adnroid.intent.action.SENDTO");        intent.addCategory("android.intent.category.DEFAULT");        intent.setData(Uri.parse("sms:110"));        startActivity(intent);    }}

OtherScreenActivity.java

package com.example.twoactivity;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.widget.Toast;/** * activity是系統的重要組件之一 作業系統要想找到activity,就必須在資訊清單檔裡面配置 *  * */public class OtherScreenActivity extends Activity {    // 重寫activity的Oncreate方法    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_two);        Intent intent = getIntent();// 擷取到啟用他的意圖        Uri uri = intent.getData();        String result = uri.toString();        Toast.makeText(this, "資料是:" + result, 0).show();    }}

並且在清淡檔案裡面AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.ce"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".ResultActivity"            android:label="@string/app_name" >        </activity>    </application></manifest>

 

Android 學習之顯式啟用與隱式啟用Activity

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.