Android 在不同Actitity之間資料傳遞,androidactitity

來源:互聯網
上載者:User

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);    }}

 

聯繫我們

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