android初學1,Activity切換

來源:互聯網
上載者:User

這兩天閑下來,想隨便寫點android程式,突然發現,之前忙了大半年的android,居然連個簡單的遊戲都做不了,看來基礎確實很重要,開始從最實學習。今天研究兩個activity切換,應該用到startActivity(Intent aIntent)。

兩個activity儘可能簡單,第一個包含一個button按鈕,點擊按鈕,調用startActivity()進行切換。

先看AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.szkingdom.uinttest"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk android:minSdkVersion="8" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

            android:label="@string/app_name"

            android:name=".Sample8Activity" >

            <intent-filter >

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        

        <activity

            android:label="@string/app_name"

            android:name=".SecondActivity" >

            <intent-filter >

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 再看第一個activity,由於需要一個響應按鈕事件,因此在onCreate中需加上如下兩行代碼:

// Watch for button clicks

Button button = (Button)findViewById(R.id.button1);

button.setOnClickListener(mFadeListener);

通過這個內部匿名類實現按鈕響應,轉到第二activity中:

private OnClickListener mFadeListener = new OnClickListener() {

    public void onClick(View v) {

        startActivity(new Intent(Sample8Activity.this, SecondActivity.class));

    }

};

 

第一個activity的代碼如下所示:

package com.szkingdom.uinttest;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class Sample8Activity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        // Watch for button clicks

        Button button = (Button)findViewById(R.id.button1);

        button.setOnClickListener(mFadeListener);

        

    }

   

    private OnClickListener mFadeListener = new OnClickListener() {

        public void onClick(View v) {

            startActivity(new Intent(Sample8Activity.this, SecondActivity.class));

        }

    };

}

 

第二個activity代碼只是一個簡單的顯示,如下所示:

package com.szkingdom.uinttest;

 

import android.app.Activity;

import android.os.Bundle;

 

public class SecondActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mainsecond);

    }

}

 

相應的,layout中兩個檔案main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello" />

 

    <Button

        android:id="@+id/button1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Button" />

 

</LinearLayout>

 

mainsecond.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/helloSecond" />

 

</LinearLayout>

 strings.xml檔案

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="hello">Hello World, Sample8Activity!</string>

    <string name="helloSecond">Hello World, helloSecond!</string>

    <string name="app_name">Sample8</string>

 

</resources>

 

至此,一個簡單的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.