編輯:關於 Android 程式設計
在 Activity 之間跳轉時傳遞資料是日常開發的常見操作。本文以一個簡單的「姻緣計算器」為例,示範兩種傳遞方式:透過 Intent 直接裝入,或先封裝進 Bundle 再交給 Intent。
在主畫面中,把要傳遞的資料封裝進 Intent(或直接放入 Bundle 後再交給 Intent),然後啟動 SecondActivity:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
Intent intent = new Intent(this, SecondActivity.class);
// 方式一:直接放入 intent
// intent.putExtra("malename", "張三");
// intent.putExtra("femalename", "芙蓉姐姐");
// 方式二:先封裝至 bundle,再放入 intent
Bundle bundle = new Bundle();
bundle.putString("malename", "張三");
bundle.putString("femalename", "芙蓉姐姐");
intent.putExtras(bundle);
startActivity(intent);
}
}
在 SecondActivity 中透過 getIntent() 取出資料,一樣可分為直接取出或先取 Bundle 再取值兩種寫法:
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
// 方式一:直接取出
// String maleName = intent.getStringExtra("malename");
// String feMaleName = intent.getStringExtra("femalename");
// 方式二:先取 bundle 再取值
Bundle bundle = intent.getExtras();
String maleName = bundle.getString("malename");
String feMaleName = bundle.getString("femalename");
Random rd = new Random();
int yinyuan = rd.nextInt(100);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(maleName + "和" + feMaleName + "的姻緣值為" + yinyuan);
}
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是姻緣計算器,很準的喲" />
<EditText
android:id="@+id/et_malename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="張三" />
<EditText
android:id="@+id/et_femalename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="芙蓉姐姐" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="計算" />
</LinearLayout>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是第二個 Activity" />
</RelativeLayout>
兩種方式效果相同:Bundle 的好處是能一次封裝多個鍵值對、結構更清晰,適合資料量較大的情境;而直接 putExtra 則更為簡潔。可依實際需求挑選。
Android MVP 模式:ListView 嵌入 CheckBox 實作
MVP 分工 本文以 MVP 模式實作 ListView 中的 CheckBox。View 負責畫面與事件,Presenter 協調資料流程,Model 提供資料來源。透
在 Android Studio 上顯示百度地圖:完整申請與整合流程
在 Eclipse 上接百度地圖從沒出過什麼亂子,換到 Android Studio 卻踩了一堆坑。加上百度地圖的官方文件實在算不上友善,於是乾脆把整個流程重新整理一遍,
Retrofit 進階(三):批量上傳與上傳進度監聽
由於 gif 圖較大,我把圖放在了 GitHub 上,博客裡若顯示不出來可以點擊傳送門查看。本文代碼寫於 md,可讀性一般,敬請諒解。 1. 前言 上一篇我們介紹了 Re
Android TagFlowLayout 標籤布局完全解析
FlowLayout 適合排列標籤,但在商品選擇、搜尋關鍵字等互動場景中,還需要處理選取、回呼與狀態保存。TagFlowLayout 在 FlowLayout 基礎上加入