android 开发之Activity切换

演示从一个Activity切换到另外的一个Activity.

 android 开发之Activity切换

第一个Activity

 android 开发之Activity切换


 ActivitySecond.java代码


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  ActivitySecond  extends  Activity
{
    
private  Button btn2;
    
private  Button btnClose;

    @Override
    
protected   void  onCreate(Bundle savedInstanceState)
    {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        btn2 
=  (Button) findViewById(R.id.btn_back);
        btn2.setOnClickListener(
new  OnClickListener()
        {

            
public   void  onClick(View v)
            {
                Intent intent 
=   new  Intent();
                intent.setClass(ActivitySecond.
this , ActivityChange. class );
                startActivity(intent);
                
// 如果不关闭当前的会出现好多个页面
                ActivitySecond. this .finish();
            }

        });
        btnClose 
=  (Button) findViewById(R.id.btn_close);
        btnClose.setOnClickListener(
new  OnClickListener()
        {

            
public   void  onClick(View v)
            {
                finish();
            }

        });
    }

}

 

 ActivityChange.java代码


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  ActivityChange  extends  Activity {
    
    
private  Button btn;
    
private  Button btnClose;
    
    @Override
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn
= (Button)findViewById(R.id.btnNextView);
        btn.setOnClickListener(
new  OnClickListener()
        {
            
public   void  onClick(View v)
            {
                Intent itent
= new  Intent();
                itent.setClass(ActivityChange.
this , ActivitySecond. class );
                startActivity(itent);
                ActivityChange.
this .finish();
            }
        });
        
        btnClose
= (Button)findViewById(R.id.btn_exit);
        btnClose.setOnClickListener(
new  OnClickListener()
        {
            
public   void  onClick(View v)
            {
                finish();
            }
        });
    }
}

 

main.xml代码
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    
>
< TextView  
    
android:layout_width ="fill_parent"  
    android:layout_height
="wrap_content"  
    android:text
="@string/hello"
    
/>
< Button 
    
android:id ="@+id/btnNextView"
    android:layout_width
="wrap_content"
    android:layout_height
="wrap_content"
    android:text
="@string/show_next"
/>

< Button 
    
android:id ="@+id/btn_exit"
    android:layout_width
="wrap_content"
    android:layout_height
="wrap_content"
    android:text
="@string/btn_close"
/>
</ LinearLayout >

 

 second_activity.xml代码

 

AndroidManifest.xml代码
<? xml version="1.0" encoding="utf-8" ?>
< manifest  xmlns:android ="http://schemas.android.com/apk/res/android"
      package
="activity.change"
      android:versionCode
="1"
      android:versionName
="1.0" >
    
< application  android:icon ="@drawable/icon"  android:label ="@string/app_name" >
        
< activity  android:name =".ActivityChange"
                  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 ="ActivitySecond"
        
/>
    
</ application >

</ manifest >  

 

 

 

 

 

 

 

你可能感兴趣的:(Android 开发)