Android beginner 1, activity Switching

Source: Internet
Author: User

I was idle for the past two days and wanted to write some Android program. I suddenly found that I had been busy with Android for more than half a year and could not even do a simple game. It seems that the Foundation is really important, start from the most practical learning. Startactivity (intent aintent) is used to switch between two activities ).

The two activities are as simple as possible. The first one contains a button, click the button, and call startactivity () to switch.

First look at 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>

Let's look at the first activity. Because we need a response button event, we need to add the following two lines of code in oncreate:

// Watch for button clicks

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

button.setOnClickListener(mFadeListener);

Use this internal Anonymous class to implement button response and go to the second activity:

private OnClickListener mFadeListener = new OnClickListener() {

    public void onClick(View v) {

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

    }

};

 

The code for the first activity is as follows:

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));

        }

    };

}

 

The second activity code is just a simple display, as shown below:

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);

    }

}

 

Correspondingly, the two files main. XML in Layout

<?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 file

<?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>

 

So far, a simple activity switching is complete. It is very detailed as it is an entry-level activity. It is very suitable for friends who are just like me and wish to help everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.