两个Activity切换例子

Activity01
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity01 extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  /* 设置显示main.xml布局 */
        
      
        Button button = (Button)this.findViewById(R.id.button1);
        button.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v){
        		//新建一个Intent
        		Intent intent = new Intent();
        		//制定intent要启动的类
        		intent.setClass(Activity01.this, Activity02.class);
        		//启动一个新的Activity
        		startActivity(intent);
        		//关闭当前的
        		Activity01.this.finish();
        		
        	}
        });
    }
    
    
}

Activity02

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity02 extends Activity{			/*savedInstanceState 保存当前Activity的状态信息。*/
	
	public void onCreate(Bundle savedInstanceState)   
	{	
		super.onCreate(savedInstanceState);			//调用父类的onCreate构造函数) 
													/* 设置显示main2.xml布局 */
		setContentView(R.layout.main2);
												 	/* findViewById(R.id.button2)取得布局main.xml中的button2 */
		Button button = (Button) findViewById(R.id.button2);
													/* 监听button的事件信息 */
		button.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
													/* 新建一个Intent对象 */
				Intent intent = new Intent();
				/* 指定intent要启动的类 */
				intent.setClass(Activity02.this, Activity01.class);
													/* 启动一个新的Activity */
				startActivity(intent);
													/* 关闭当前的Activity */
				Activity02.this.finish();
			}
			
		});
	}
}

strings.xml

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

    <string name="hello">第一个Activity</string>
    <string name="hello2">第二个Activity</string>    
    <string name="app_name">Activity切换</string>

</resources>


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:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_x = "120px"
        android:layout_y = "90px"
        android:text ="切换"
        android:id="@+id/button1"
        >
     </Button>   

</LinearLayout>


main2.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/hello2" 
    />
    <Button
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_x = "120px"
        android:layout_y = "90px"
        android:text ="切换"
        android:id="@+id/button2"
        >
     </Button>   

</LinearLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.lysine"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Activity01"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name="Activity02">
         </activity>
        
        
    </application>

</manifest>



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值