我的Android筆記(三)——使用Intent在Activity間傳值

來源:互聯網
上載者:User

在Android中, 一個Activity可以理解為一個螢幕。  當程式要從一個Activity切換到另外一個Activity時,就需要用到Intent。

Intent專門負責Android程式之間、以及程式內Activity與Service之間互動、通訊。

 


以下是一個簡單的demo,使用Intent從一個Activity切換到另一個Activity,並且傳遞一個資料進行顯示。

 


先看一下——

    第一個Activity:

 
輸入一個姓名“Barry”:

點擊“確定”,跳轉到另一個Activity,並顯示“你好,Barry!”:

 
代碼如下:
  
 

Demo_intentActivity.java

 

[java]
package barry.android.intent; 
 
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.EditText; 
 
public class Demo_intentActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        Button btn = (Button) findViewById(R.id.button1); 
        btn.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                String yourName = ((EditText) findViewById(R.id.name_text)) 
                        .getText().toString(); 
                Intent intent = new Intent(); 
                intent.setClass(Demo_intentActivity.this, RecordActivity.class); 
                intent.putExtra("name", yourName);// 在Intent中設定要傳遞的資料  
                startActivity(intent);// 啟動新的Activity  
                Demo_intentActivity.this.finish();// 結束就的Activity  
            } 
        }); 
    } 
} 
package barry.android.intent;

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.EditText;

public class Demo_intentActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btn = (Button) findViewById(R.id.button1);
  btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    String yourName = ((EditText) findViewById(R.id.name_text))
      .getText().toString();
    Intent intent = new Intent();
    intent.setClass(Demo_intentActivity.this, RecordActivity.class);
    intent.putExtra("name", yourName);// 在Intent中設定要傳遞的資料
    startActivity(intent);// 啟動新的Activity
    Demo_intentActivity.this.finish();// 結束就的Activity
   }
  });
 }
}以及它的布局檔案main.xml


[html]
<?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" > 
 
    <EditText  
        android:id="@+id/name_text" 
        android:hint="輸入你的名字" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />     
     
    <Button android:text="確定" 
        android:id="@+id/button1" 
        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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/name_text"
        android:hint="輸入你的名字"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />   
   
    <Button android:text="確定"
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>   
   
</LinearLayout>

 


另一個Activity  RecordActivity.java

[java]
package barry.android.intent; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class RecordActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.record);         
        Intent intent = getIntent(); 
        String yourname = intent.getStringExtra("name");//從Intent中擷取資料          
        TextView text_show = (TextView) findViewById(R.id.text_show); 
        text_show.setText("你好,"+yourname+"!"); //顯示到螢幕上  
    } 
} 
package barry.android.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class RecordActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.record);       
        Intent intent = getIntent();
        String yourname = intent.getStringExtra("name");//從Intent中擷取資料       
        TextView text_show = (TextView) findViewById(R.id.text_show);
        text_show.setText("你好,"+yourname+"!"); //顯示到螢幕上
    }
}
及其布局檔案 record.xml


[html]
<?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:id="@+id/text_show" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 
 
</LinearLayout> 
<?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:id="@+id/text_show"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

需要注意的是,每個Activity都需要在AndroidManiFest.xml中聲明一下,所以在AndroidManiFest.xml中需要加入:


[html]
<activity android:name=".RecordActivity"></activity> 
 
摘自  狼的第二個小窩
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.