實現android activity之間的跳轉

來源:互聯網
上載者:User

標籤:

android程式一般不會只有一個activity,會碰到activity之間的跳轉。以下是使用Intent做應用程式內部的activity做跳轉。比如,應用程式第一個activity是:

點擊“下一步”按鈕後:

這需要寫兩個Activity類。第一個是:MainActivity

 1 package com.easymorse; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainActivity extends Activity {11 12     private Button button;13 14     /** Called when the activity is first created. */15     @Override16     public void onCreate(Bundle savedInstanceState) {17         super.onCreate(savedInstanceState);18         setContentView(R.layout.main);19 20         this.button = (Button) this.findViewById(R.id.Button01);21         this.button.setOnClickListener(new OnClickListener() {22             @Override23             public void onClick(View v) {24                 Intent intent = new Intent();25                 intent.setClass(MainActivity.this, NextActivity.class);26                 startActivity(intent);27             }28         });29     }30 }

第二個是:NextActivity

 1 package com.easymorse; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5  6 public class NextActivity extends Activity { 7     @Override 8     protected void onCreate(Bundle savedInstanceState) { 9         super.onCreate(savedInstanceState);10         this.setContentView(R.layout.next_activity);11     }12 }

然後,要在AndroidManifest.xml中增加這兩個Activity的聲明。

 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3       package="com.easymorse" 4       android:versionCode="1" 5       android:versionName="1.0"> 6     <application android:icon="@drawable/icon" android:label="@string/app_name"> 7         <activity android:name=".MainActivity" 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 15     <activity android:name="NextActivity"></activity>16 </application>17     <uses-sdk android:minSdkVersion="3" />18 19 </manifest> 

在string.xml中增加常量字串:

1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3     <string name="hello">Hello World, MainActivity!</string>4     <string name="app_name">activity.forward.demo</string>5     <string name="next_button">下一步</string>6 </resources>

layout目錄下除了原有的main.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   8     android:layout_width="fill_parent"  9     android:layout_height="wrap_content" 10     android:text="@string/hello"11     />12 <Button android:text="@string/next_button" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>13 </LinearLayout>

還需要建立NextActivity的布局檔案聲明,比如next_activity.xml:

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

這種情況下,如果按回退鍵將回到MainActivity。如果不希望回退到前一個activity,而是退出。需要這樣:

 1         this.button = (Button) this.findViewById(R.id.Button01); 2         this.button.setOnClickListener(new OnClickListener() { 3             @Override 4             public void onClick(View v) { 5                 Intent intent = new Intent(); 6                 intent.setClass(MainActivity.this, NextActivity.class); 7                 startActivity(intent); 8                 finish(); 9             }10         });

實現activity之間的跳轉.rar

實現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.