Android開發入門(五)之介面Activity之間的跳轉

來源:互聯網
上載者:User
原文地址:http://www.cmath.net/read.php?tid=57在開始讀這篇文章之間,介意你先閱讀Android開發入門(四)之介面Activity。

Activity之間的跳轉分為兩種一種是顯示的,另一種是隱式跳轉,推介使用隱式跳轉,不過本文兩種都會講解。

在Android開發入門(四)之介面Activity中源碼中已包含了顯式跳轉。

首先在Android開發入門(四)之介面Activity中已有一個Activity,這裡需在再建一個Activity。
第一步:在項目中res/layout/下添加一個display.xml,內容為


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="@string/hello" 
    android:id="@+id/DisplayName"/>
  8. </LinearLayout>

第二步:在com.cmath包下添加一個名叫Display的Activity類,Display.java內容為


  1. package com.cmath;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class Display extends Activity {
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.display);
  11.         Intent intent = getIntent();
  12.         Bundle bundle = intent.getExtras();
  13.         TextView text = (TextView) findViewById(R.id.DisplayName);
  14.         text.setText("name:" + bundle.getString("name"));
  15.     }
  16. }

這樣一個新的Activity就完成了

因為在該項止中我們在Android開發入門(四)之介面Activity中已建了一個main的Activity,而這裡我們的目的就是為了從main跳到Display中。

在main這個Activity中已有一個id為Submit的Button,當點擊這個Button後要跳到Display中。修改main.java的代碼為


  1. package com.cmath;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. public class main extends Activity {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.         final EditText name = (EditText) findViewById(R.id.TextName);
  15.         final Button submit = (Button) findViewById(R.id.Submit);
  16.         submit.setOnClickListener(new Button.OnClickListener() {
  17.             @Override
  18.             public void onClick(View v) {
  19.                 Intent intent = new Intent();
  20.                 intent.putExtra("name",
    name.getText().toString());//添加一些內容,傳給Display
  21.                 intent.setClass(v.getContext(),
    Display.class);//指明要跳轉的Activity類
  22.                 startActivity(intent);
  23.             }
  24.         });
  25.     }
  26. }

在上面代碼中啟動跳轉作用的是


  1. intent.setClass(v.getContext(), Display.class);//指明要跳轉的Activity類
  2. startActivity(intent);

在intent.setClass方法中指明跳到哪個Activity,v.getContext()指當前的Activity,Display.class指將在跳到的Activity
startActivity(intent)
執行跳轉。

下面介紹隱式的跳轉,隱式的跳轉會出顯式的跳轉複雜一點,但是會更有用!
首先說到隱式跳轉,會使用到ContentProvider。ContentProvider會在以後的文章中說到,這裡只是簡單說明。
第一步:在com.cmath包下建立CmathProvide.java,內容為:package
com.cmath;


  1. import android.content.ContentProvider;
  2. import android.content.ContentValues;
  3. import android.database.Cursor;
  4. import android.net.Uri;
  5. import android.util.Log;
  6. public class CmathProvider extends ContentProvider {
  7.     @Override
  8.     public int delete(Uri arg0, String arg1, String[] arg2) {
  9.         // TODO Auto-generated method stub
  10.         Log.e("fffff", "fffff");
  11.         return 0;
  12.     }
  13.     @Override
  14.     public String getType(Uri uri) {
  15.         return "vnd.cmath.name/vnd.cn.cmath";
  16.     }
  17.     @Override
  18.     public Uri insert(Uri uri, ContentValues values) {
  19.         // TODO Auto-generated method stub
  20.         Log.e("ddddd", "dddd");
  21.         return null;
  22.     }
  23.     @Override
  24.     public boolean onCreate() {
  25.         // TODO Auto-generated method stub
  26.         Log.e("cccc", "ccccc");
  27.         return false;
  28.     }
  29.     @Override
  30.     public Cursor query(Uri uri, String[] projection, String selection,
  31.             String[] selectionArgs, String sortOrder) {
  32.         Log.e("bbbbb", "bbbb");
  33.         return null;
  34.     }
  35.     @Override
  36.     public int update(Uri uri, ContentValues values, String selection,
  37.             String[] selectionArgs) {
  38.         Log.e("aaaa", "bbbb");
  39.         return 0;
  40.     }
  41. }

這些方法很簡單,都是Override
ContentProvider
的方法,有一定程式經驗的一看就知道,這些都是CRUD方法,ContentProvider就是內容管理。

第二步:修改項目根目錄下的AndroidManifest.xml檔案為:


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.cmath"
  4.       android:versionCode="1"
  5.       android:versionName="1.0.0">
  6.     <application android:icon="@drawable/icon"
    android:label="@string/app_name">
  7.      
  8.      <provider android:name="CmathProvider"
    android:authorities="com.cmath.provider.name"/>
  9.    
  10.         <activity android:name=".main"
    android:label="@string/app_name">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER"
    />
  14.             </intent-filter>
  15.         </activity>
  16.         <activity android:name=".Display">
  17.          <intent-filter>
  18.                 <action android:name="android.intent.action.EDIT" />
  19.                 <category android:name="android.intent.category.DEFAULT"
    />
  20.                 <data android:mimeType="vnd.cmath.name/vnd.cn.cmath"
    />
  21.          </intent-filter>
  22.         </activity>
  23.     </application>
  24. </manifest>

第三步:修改main中button的處理事件代碼


  1. submit.setOnClickListener(new Button.OnClickListener() {
  2.             @Override
  3.             public void onClick(View v) {
  4.                 Intent intent = new Intent();
  5.                 intent.putExtra("name", name.getText().toString());
  6.                 intent.setAction(Intent.ACTION_EDIT);
  7.                
    intent.setData(Uri.parse("content://com.cmath.provider.name/test"));
  8.                 startActivity(intent);
  9.             }
  10.         });

到這裡代碼修改就完成了。
下面來具體講解一下整個程式的運行過程。在講解之前在說明一下,Android的隱式跳轉是基於事件的,有系統預定義的,也可以自訂。
首先我們從這個程式開始後按下main中的Button說起。
第一步:按下Button,執行上面onClick方法,在這個方法中初始化Intent,然後調用setAction添加EDIT事件,setData設定Uri。最後調用startActivity。
第二步:當調用startActivity後,一切都Android內部處理了。
              
到了這裡需在明白AndroidManifest.xml中
 


  1. <provider android:name="CmathProvider"
    android:authorities="com.cmath.provider.name"/>

這是一個Provider配置:android:name指類名,就是我們建的CmathProvider類(包名在manifest
xmlns標籤中已指明)
可能到了這裡有人已發現


  1. intent.setData(Uri.parse("content://com.cmath.provider.name/test"));

中有內容和android:authorities內容相同,的確


  1. content://com.cmath.provider.name/test

是一個固定格式,以content://
開始,再加一個Provider的android:authorities,以某個自訂字元結束,就構成了


  1. content://[android:authorities]/[notes]

上面程式中Android會根據content://com.cmath.provider.name/test中的"com.cmath.provider.name"去匹配android:authorities,
就到找到CmathProvider,再調用getType方法。

第三步:getType返回一個"vnd.cmath.name/vnd.cn.cmath“
             
再看看activity配置


  1. <activity android:name=".Display">
  2.          <intent-filter>
  3.                 <action android:name="android.intent.action.EDIT" />
  4.                 <category android:name="android.intent.category.DEFAULT"
    />
  5.                 <data android:mimeType="vnd.cmath.name/vnd.cn.cmath"
    />
  6.          </intent-filter>
  7. </activity>

根據getType返回的"vnd.cmath.name/vnd.cn.cmath“

intent.setAction(Intent.ACTION_EDIT)可以找到Display這個Activity。這樣就可以確定要跳轉的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.