Android開發中插入新的Activity

來源:互聯網
上載者:User

一個Activity就相當於程式的一頁,如果想要跳轉到新的一頁,就必須插入新的Activity。

插入新的Activity有三步驟:

1、建立新的Activity程式碼,這裡以”new.class”為例

2、在AndroidManifest.xml中添加新Activity的描述

3、在原有Activity中調用啟動新的Activity

下面一步一步來,首先建立新的Activity程式碼:

Eclipse左側的Package Explorer中的src下的package上點右鍵,New一個Class。有一點要注意,在彈出的對話方塊中,Superclass要選擇Activity,Name必須大寫(這是JAVA的規定,必須這樣,否則無法建立)

,在建立的Name.class裡插入代碼:

 
  1. public class Name extends Activity {   
  2. @Override   
  3. protected void onCreate(Bundle savedInstanceState) {   
  4. // TODO Auto-generated method stub   
  5. super.onCreate(savedInstanceState);   
  6. setContentView(R.layout.newviewxml);   
  7.     }   
  8. }   

然後建立相應的描述UI的xml檔案,

右鍵點擊 project/res/layout ,在layoutnew->Android xml file

格式複製原有main.xml的格式即可,根據上面的代碼(R.layout.newviewxml),這個xml檔案名稱應該為newviewxml.xml

接下來第二步,在AndroidManifest.xml中添加新Activity的描述

開啟AndroidManifest.xml,切換到Application頁面,在Application Nodes裡,列出了這個程式目前所有的Activity(當然不包括我們現在要添加的),點右邊的Add,

點擊OK,開啟AndroidManifest.xml,加入

 
  1. <activity android:name="Name"></activity>  

然後是在原有Activity中調用啟動新的Activity

 
  1. Intent intent=new Intent();   
  2.  
  3. intent.setClass(Test.this,Name.class);   
  4. //當前的Activity為Test,目標Activity為Name //從下面這行開始是將資料傳給新的Activity,如果不傳資料,只是簡單的跳轉,這幾行代碼請注釋掉   
  5. Bundle bundle=new Bundle()   
  6. bundle.putString("key1","value1");//key1為名,value1為值   
  7. bundle.putString("key2","value2");   
  8. intent.putExtras(bundle);//傳資料結束   
  9. startActivity(intent);  

到這裡,新的Activity就被制調用了,如果剛才在原Activity中傳送了資料,用下面的代碼可以在新的Activity中擷取到。

 
  1. Bundle bundle=this.getIntent().getExtras();   
  2. String s1=bundle.getString("key1");   
  3. String s2=bundle.getString("key2"); 

Android入門學習總攻略

Android學習筆記:Activity跳轉

Android開發:Activity之間的傳值

解析Android Widget開發中如何構建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.