android初学1,Activity切换

这两天闲下来,想随便写点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切换就完成了,由于是入门级的,因此非常详细,非常适合像我一样半路出家的朋友,希望对大家有帮助。

posted @ 2011-12-21 15:16  绿色的麦田  阅读(1236)  评论(0编辑  收藏  举报