Android 在不同Actitity之間資料傳遞,androidactitity
本文實現一個簡易的人品計算機來實踐在不同Actitity之間資料傳遞
intent的資料傳遞
從A介面開啟B介面 把A介面的資料傳遞給B介面
1. intent.setData(uri) -- intent.getData();
可以傳遞簡單的文本資料。
2. intent.putExtra(name, value)
8大基本類型的資料,數組都可以傳遞
String對象 可以傳遞 charSequence
可以序列化的對象(序列化到檔案) Serializable 也可以傳遞
可以序列化的對象(序列化到記憶體) Parcelable 也可以傳遞 bitmap
可以傳遞 一個map集合 Bundle extras = new Bundle();
本文地址:http://www.cnblogs.com/wuyudong/p/5679191.html,轉載請註明源地址。
建立項目,activity_main.xml中的代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="人品計算機" android:textColor="#ff0000" android:textSize="26sp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入姓名" > </EditText> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_centerInParent="true" android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="計算" > </Button> </RelativeLayout></LinearLayout>
介面如下:
輸入姓名後,點擊按鈕跳轉到另一個結果介面:activity_result.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="測試結果:" android:textColor="#ff0000" android:textSize="20sp" /> <TextView android:id="@+id/tv_result" android:text="xxx:" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#99000000" android:textSize="17sp" /> <ProgressBar android:id="@+id/progressBar1" android:max="100" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
介面如下:
ResultActivity.java代碼如下:
package com.wuyudong.testrp;import java.util.Random;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.ProgressBar;import android.widget.TextView;public class ResultActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); TextView tv_result = (TextView) findViewById(R.id.tv_result); Intent intent = getIntent(); String name = intent.getStringExtra("name"); Random rm = new Random(); int rp = rm.nextInt(101); tv_result.setText(name + ":您的人品值為:" + rp); ProgressBar pb = (ProgressBar)findViewById(R.id.progressBar1); pb.setProgress(rp); }}