Android Activity跳轉相關操作技巧分享

來源:互聯網
上載者:User

我們曾經在一篇文章中為大家詳細介紹過有關Android Activity的相關內容。在這裡,我們會進一步的對這一操作進行詳細剖析。主要就是針對Android Activity跳轉的實現。下面就讓我們一起看看具體操作方法吧。

Android中提供一個叫Intent的類來實現螢幕之間的跳轉,下面是一個簡單的樣本:

在應用中增加一個Activity名字為.ForwardTarget),這需要修改AndroidManifest.xml檔案,如下:

Android Activity跳轉程式碼範例:

  1. < ?xml version="1.0" encoding="utf-8"?>   
  2. < manifest xmlns:android="< A href="http://schemas.android.com/
    apk/res/android">http://schemas.android.com/apk/res/android< /A>"   
  3. package="com.ray.forward"   
  4. android:versionCode="1"   
  5. android:versionName="1.0">   
  6. < application android:icon="@drawable/icon" 
    android:label="@string/app_name">   
  7. < activity android:name=".androidForward"   
  8. android:label="@string/app_name">   
  9. < intent-filter>   
  10. < action android:name="android.intent.action.MAIN" />   
  11. < category android:name="android.intent.category.LAUNCHER" />   
  12. < /intent-filter>   
  13. < /activity>   
  14. < activity android:name=".ForwardTarget">   
  15. < /activity>   
  16. < /application>   
  17. < uses-sdk android:minSdkVersion="3" />   
  18. < /manifest>   
  19. < ?xml version="1.0" encoding="utf-8"?> 
  20. < manifest xmlns:android=
    "http://schemas.android.com/apk/res/android" 
  21. package="com.ray.forward" 
  22. android:versionCode="1" 
  23. android:versionName="1.0"> 
  24. < application android:icon="@drawable/icon" 
    android:label="@string/app_name"> 
  25. < activity android:name=".androidForward" 
  26. android:label="@string/app_name"> 
  27. < intent-filter> 
  28. < action android:name="android.intent.action.MAIN" /> 
  29. < category android:name="android.intent.category.LAUNCHER" /> 
  30. < /intent-filter> 
  31. < /activity> 
  32. < activity android:name=".ForwardTarget"> 
  33. < /activity> 
  34. < /application> 
  35. < uses-sdk android:minSdkVersion="3" /> 
  36. < /manifest>  

然後在layout中的main加入一個id為leah1的按鈕,另外再建立一個任意的layout將要跳轉到得layout),我取名為leah1。

接下來是兩個類,一個是AndroidForward,另一個是將要跳轉到得ForwardTarget,Android Activity跳轉實現的代碼分別如下:

  1. AndroidForw:  
  2. package com.ray.forward;   
  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. public class androidForward 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. Button btn1 =(Button)findViewById(R.id.leah1);   
  15. btn1.setOnClickListener(new View.OnClickListener(){   
  16. @Override   
  17. public void onClick(View v) {   
  18. Intent intent = new Intent();   
  19. intent.setClass(androidForward.this, ForwardTarget.class);   
  20. startActivity(intent);   
  21. finish();
    //停止當前的Activity,如果不寫,則按返回鍵會跳回原來的Activity   
  22. }   
  23. });   
  24. }   
  25. }   
  26. package com.ray.forward;  
  27. import android.app.Activity;  
  28. import android.content.Intent;  
  29. import android.os.Bundle;  
  30. import android.view.View;  
  31. import android.widget.Button;  
  32. public class androidForward extends Activity {  
  33. /** Called when the activity is first created. */  
  34. @Override  
  35. public void onCreate(Bundle savedInstanceState) {  
  36. super.onCreate(savedInstanceState);  
  37. setContentView(R.layout.main);  
  38. Button btn1 =(Button)findViewById(R.id.leah1);  
  39. btn1.setOnClickListener(new View.OnClickListener(){  
  40. @Override  
  41. public void onClick(View v) {  
  42. Intent intent = new Intent();  
  43. intent.setClass(androidForward.this, ForwardTarget.class);  
  44. startActivity(intent);  
  45. finish();
    //停止當前的Activity,如果不寫,則按返回鍵會跳回原來的Activity   
  46. }  
  47. });  
  48. }  
  49. }   
  50. ForwardTarget:  
  51. package com.ray.forward;   
  52. import android.app.Activity;   
  53. import android.os.Bundle;   
  54. public class ForwardTarget extends Activity{   
  55. @Override   
  56. protected void onCreate(Bundle savedInstanceState) {   
  57. // TODO Auto-generated method stub   
  58. super.onCreate(savedInstanceState);   
  59. setContentView(R.layout.leah1);   
  60. }   
  61. } 

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.