android學習筆記11————–Activity之切換與參數傳遞

實現功能:

1.兩個Activity之間的切換

2.兩個Activity帶返回值的切換

效果圖:

 

 

點擊切換Activity,或帶返回值的Activity,出現:

 

 

點擊關閉,其中帶返回值的效果如下:

 

 

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; 
import android.widget.TextView; 
 
public class Main extends Activity 

    Button button; 
    Button button2; 
    TextView textView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        button=(Button)findViewById(R.id.button); 
        button2=(Button)findViewById(R.id.button2); 
        textView=(TextView)findViewById(R.id.display); 
         
        button.setOnClickListener(new OnClickListener() 
        {            
            @Override 
            public void onClick(View v) 
            { 
                /**
                 * 向新activity傳參數
                 */ 
                Intent intent= new Intent(Main.this, OtherActivity.class); 
                intent.putExtra("age", 10); 
                intent.putExtra("name", "lingming"); 
                Main.this.startActivity(intent); 
            } 
        }); 
         
        button2.setOnClickListener(new OnClickListener() 
        {            
            @Override 
            public void onClick(View v) 
            { 
                /**
                 * 帶返回值的Activity切換
                 */ 
                Intent intent= new Intent(Main.this, OtherActivity.class); 
                intent.putExtra("age", 10); 
                intent.putExtra("name", "lingming"); 
                Main.this.startActivityForResult(intent, 3);//3是標示請求碼,隨便寫  
            } 
        }); 
    } 
    /**
     *  接收帶有返回值的Activity方法
     */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
        super.onActivityResult(requestCode, resultCode, data); 
        textView.setText("requestCode:"+requestCode+'\n'+ 
                         "resultCode:"+resultCode+'\n'+ 
                         "接收到:"+data.getStringExtra("返回值") 
        ); 
    } 
     

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;
import android.widget.TextView;

public class Main extends Activity
{
 Button button;
 Button button2;
 TextView textView;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  button=(Button)findViewById(R.id.button);
  button2=(Button)findViewById(R.id.button2);
  textView=(TextView)findViewById(R.id.display);
  
  button.setOnClickListener(new OnClickListener()
  {   
   @Override
   public void onClick(View v)
   {
    /**
     * 向新activity傳參數
     */
    Intent intent= new Intent(Main.this, OtherActivity.class);
    intent.putExtra("age", 10);
    intent.putExtra("name", "lingming");
    Main.this.startActivity(intent);
   }
  });
  
  button2.setOnClickListener(new OnClickListener()
  {   
   @Override
   public void onClick(View v)
   {
    /**
     * 帶返回值的Activity切換
     */
    Intent intent= new Intent(Main.this, OtherActivity.class);
    intent.putExtra("age", 10);
    intent.putExtra("name", "lingming");
    Main.this.startActivityForResult(intent, 3);//3是標示請求碼,隨便寫
   }
  });
 }
 /**
  *  接收帶有返回值的Activity方法
  */
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
  super.onActivityResult(requestCode, resultCode, data);
  textView.setText("requestCode:"+requestCode+'\n'+
       "resultCode:"+resultCode+'\n'+
       "接收到:"+data.getStringExtra("返回值")
  );
 }
 
}

 

[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; 
import android.widget.TextView; 
 
public class OtherActivity extends Activity 

    TextView textView; 
    Button button; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.otherui); 
        Intent intent=this.getIntent(); 
        int age=intent.getIntExtra("age", 0); 
        String name=intent.getStringExtra("name"); 
         
        textView=(TextView)findViewById(R.id.textView); 
        textView.setText("年齡:"+age+'\n'+"姓名:"+name); 
         
        button=(Button)findViewById(R.id.finish); 
        button.setOnClickListener(new OnClickListener() 
        { 
             
            @Override 
            public void onClick(View v) 
            { 
                Intent intent=new Intent(); 
                intent.putExtra("返回值", "這是返回值"); 
                OtherActivity.this.setResult(12, intent); 
                OtherActivity.this.finish(); 
                 
            } 
        }); 
         
    } 
     

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;
import android.widget.TextView;

public class OtherActivity extends Activity
{
 TextView textView;
 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.otherui);
  Intent intent=this.getIntent();
  int age=intent.getIntExtra("age", 0);
  String name=intent.getStringExtra("name");
  
  textView=(TextView)findViewById(R.id.textView);
  textView.setText("年齡:"+age+'\n'+"姓名:"+name);
  
  button=(Button)findViewById(R.id.finish);
  button.setOnClickListener(new OnClickListener()
  {
   
   @Override
   public void onClick(View v)
   {
    Intent intent=new Intent();
    intent.putExtra("返回值", "這是返回值");
    OtherActivity.this.setResult(12, intent);
    OtherActivity.this.finish();
    
   }
  });
  
 }
 
}

 

main.xml

[java]
<?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="這是MainActivity" 
    /> 
<Button  
        android:text="切換Activity"  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"></Button> 
<Button  
        android:text="帶返回值的Activity"  
        android:id="@+id/button2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"></Button> 
<TextView   
    android:id="@+id/display" 
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    />        
</LinearLayout> 
<?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="這是MainActivity"
    />
<Button
  android:text="切換Activity"
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></Button>
<Button
  android:text="帶返回值的Activity"
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></Button>
<TextView 
 android:id="@+id/display"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />  
</LinearLayout>
 

 

otherui.xml

[java]
<?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="這是新的Activity" 
    /> 
<TextView  
    android:id="@+id/textView"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"></TextView> 
<Button  
    android:text="關閉"  
    android:id="@+id/finish"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"></Button> 
     
</LinearLayout> 
<?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="這是新的Activity"
    />
<TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"></TextView>
<Button
 android:text="關閉"
 android:id="@+id/finish"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"></Button>
 
</LinearLayout>

 

註意第二個Activity要在AndroidMainfest.xml中進行配置

[java]
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.luku.mulActivity" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".Main" 
                  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=".OtherActivity" 
                  android:label="@string/app_name"> 
                   
        </activity> 
         
    </application> 
</manifest> 

摘自 奔跑的蝸牛

發佈留言