android:Activity資料傳遞之基礎資料型別 (Elementary Data Type)

來源:互聯網
上載者:User

android:Activity資料傳遞之基礎資料型別 (Elementary Data Type)

既然是activity之間的資料傳遞 肯定有兩個activity 我們先建立兩個activity,在MainActivity中

添加一個按鈕組件 點擊按鈕 就跳轉到其它的Activity 實現資料的傳遞
實現activity之間的跳轉可以通過顯來實現,像這樣

Intent intent=new Intent();intent.setClass(MainActivity.this, OtherActivity.class);startActivity(intent);

但是我們如何要把資料傳遞過去呢?我們可以通過putExtra()方法傳遞。在這裡我們傳遞一個string類型的資料
intent.putExtra("姓名", "我是蘇蘇");

這裡就類似與map集合,其中"姓名"是鍵,"我是蘇蘇"就是值,一對一的關係。通過putExtra()方法把string存在intent裡面

在OtherActivity裡面通過getIntent().getExtra()來獲得Intent對象裡面的資料,getExtra()返回的是Bundle對象

通過Bundle對象接收,然後在記錄檔中列印

 

Bundle bundle=getIntent().getExtras();String name=bundle.getString("姓名");Toast.makeText(OtherActivity.this, name, Toast.LENGTH_LONG).show();
運行結果如下 可以發現我們把資料從一個Activity傳遞到了另外一個activity

當然也可以傳遞其它的基礎資料型別 (Elementary Data Type) 比如Double int等等 只要再接收的時候改變一下 比如接收double類型的

Double x=bundle.getDouble(key);

附上代碼

MainActivity

package com.example.activity;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button) findViewById(R.id.button1);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setClass(MainActivity.this, OtherActivity.class);intent.putExtra("姓名", "我是蘇蘇");startActivity(intent);}});}}

OtherActivity
package com.example.activity;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.widget.Toast;public class OtherActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_other);Bundle bundle=getIntent().getExtras();String name=bundle.getString("姓名");Toast.makeText(OtherActivity.this, name, Toast.LENGTH_LONG).show();}}

activity_main.xml


activity_other.xml
<!--?xml version="1.0" encoding="utf-8"?--><linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"></linearlayout>
ActivityManifest
<!--?xml version="1.0" encoding="utf-8"?--><manifest android:versioncode="1" android:versionname="1.0" package="com.example.activity" xmlns:android="http://schemas.android.com/apk/res/android">    <uses-sdk android:minsdkversion="8" android:targetsdkversion="18">    <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">        <activity android:label="@string/app_name" android:name="com.example.activity.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN">                <category android:name="android.intent.category.LAUNCHER">            </category></action></intent-filter>        </activity>        <activity android:name="com.example.activity.OtherActivity"></activity>    </application></uses-sdk></manifest>

聯繫我們

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