白手起家之android學習第一天

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   java   

intent學習---nothingcpd

  簡要介紹,intent就是activity之間通訊的介質,通過intent可以進行activity的喚起,資料的傳輸等,總之要進行多個activity互動,必須學會intent,本章只是一個很簡單的intent,後面會陸續更新複雜的應用,由於我的電腦比較慢,所以借用一下別人的代碼和過程。(wanglianghuan)

首先,在這裡稍微介紹一下意圖(Intent)的概念:

Intent(意圖)主要是解決Android應用的各項組件之間的通訊。

 

Intent 負責對應用中一次操作的動作、動作涉及資料、附加資料進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。因此,Intent在這裡起著一個媒體中介的作用,專門提供組件互相調用的相關資訊,實現調用者與被調用者之間的解耦。通過Intent實現Activity之間資料傳遞步驟如下:

1、建立兩個Activity, 分別作為發送端和接受端;

 

2、在發送端的Activity裡面建立Intent對象,給Intent對象附加資料進去;

3、在接收端通過getIntent()擷取傳遞過來的Intent對象,然後取出資料顯示到TextView。

 

下面通過本人寫的一個小例子代碼來講解,首先看一下啟動並執行效果:

發送端MainActivity:

接收端OtherActivity:

 

以下是本項目的原始碼:

一、MainActivity.java

 

 1 package com.intent.activity; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainActivity extends Activity {11     private Button btn;12     @Override13     public void onCreate(Bundle savedInstanceState) {14         super.onCreate(savedInstanceState);15         setContentView(R.layout.main);16         btn = (Button)findViewById(R.id.btOpenOtherActivity);17         btn.setOnClickListener(new OnClickListener() {18             @Override19             public void onClick(View v) {20                 //定義一個意圖21                 Intent intent = new Intent(MainActivity.this,OtherActivity.class);22                 //這裡使用Bundle來傳遞資料23                 Bundle data = new Bundle();24                 data.putString("name", "wulianghuan");25                 data.putString("age", "22");26                 data.putString("address", "上海閔行");27                 intent.putExtras(data);28                 //啟動意圖29                 startActivity(intent);30             }31         });32     }33 }

 

 

 

 

二、OtherActivity.java

 

 1 package com.intent.activity; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.TextView; 9 10 public class OtherActivity extends Activity {11     private TextView text_name;12     private TextView text_age;13     private TextView text_address;14 15     @Override16     public void onCreate(Bundle savedInstanceState) {17         super.onCreate(savedInstanceState);18         setContentView(R.layout.other);19         text_name = (TextView) findViewById(R.id.name);20         text_age = (TextView) findViewById(R.id.age);21         text_address = (TextView) findViewById(R.id.address);22         //擷取Intent傳遞的Bundle對象和它裡面的資料23         Bundle data = getIntent().getExtras();24         String name = data.getString("name");25         String age = data.getString("age");26         String address = data.getString("address");27         //設定文字框的資料28         text_name.setText("姓名:"+name);29         text_age.setText("年齡:"+age);30         text_address.setText("地址:"+address);31     }32 }

 

 

 

三、布局檔案main.xml

 

 

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6  7     <TextView 8         android:layout_width="fill_parent" 9         android:layout_height="wrap_content"10         android:text="這是:MainActivity" />11     12     <Button 13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:id="@+id/btOpenOtherActivity"16         android:text="開啟OtherActivity"/>17 18 </LinearLayout>

 

四、布局檔案other.xml

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6      7     <TextView 8         android:layout_width="fill_parent" 9         android:layout_height="wrap_content"10         android:text="這裡是OtherActivity"/>11     <TextView12         android:layout_width="fill_parent"13         android:layout_height="wrap_content"14         android:id="@+id/name"/>15     <TextView16         android:layout_width="fill_parent"17         android:layout_height="wrap_content"18         android:id="@+id/age"/>19     <TextView20         android:layout_width="fill_parent"21         android:layout_height="wrap_content"22         android:id="@+id/address"/>23     24 </LinearLayout>

 

 

 

最後注意一點:一定不要忘了在AndroidManifest.xml 檔案中對添加的Activity進行聲明哦:

1 <activity android:name=".OtherActivity"/>

 

OK,以上是簡單的intent部署,哈哈happy!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.