編輯:Android 開發實例
本範例把最常見的輸入型元件集中在一個註冊頁面上:文字標籤 TextView、單行輸入 EditText、密碼欄 EditText、單選鈕 RadioButton、多選框 CheckBox、開關鈕 ToggleButton 與下拉清單 Spinner,按下註冊後把結果帶到另一個 Activity 用 ListView 呈現。
android:password="true" 即成為密碼欄package com.amaker.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private Button register, cancel;
private ToggleButton marriged;
private RadioButton male, female;
private EditText username, password;
private Spinner position;
private CheckBox reading, swimming;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
male = (RadioButton)findViewById(R.id.male);
female = (RadioButton)findViewById(R.id.female);
reading = (CheckBox)findViewById(R.id.reading);
swimming = (CheckBox)findViewById(R.id.swimming);
marriged = (ToggleButton)findViewById(R.id.marriged);
position = (Spinner)findViewById(R.id.position);
String[] str = {"CEO", "CFO", "PM"};
ArrayAdapter aa = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, str);
position.setAdapter(aa);
register = (Button)findViewById(R.id.register);
cancel = (Button)findViewById(R.id.cancel);
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle b = new Bundle();
b.putString("username", "用戶名稱:" + username.getText().toString());
b.putString("password", "用戶密碼:" + password.getText().toString());
if (male.isChecked()) {
b.putString("gender", "性別:男");
} else {
b.putString("gender", "性別:女");
}
String temp = "愛好:";
if (reading.isChecked()) {
temp += "閱讀";
}
if (swimming.isChecked()) {
temp += " ";
temp += "游泳";
}
b.putString("hobby", temp);
if (marriged.isChecked()) {
b.putString("marriged", "婚否:已婚");
} else {
b.putString("marriged", "婚否:未婚");
}
b.putString("position", "職位:" + position.getSelectedItem().toString());
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("data", b);
startActivity(intent);
}
});
}
}
結果頁用 getBundleExtra() 把整包資料取回,攤平成字串清單後交給 ListView 顯示。
package com.amaker.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ResultActivity extends Activity {
private ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
listView = (ListView) findViewById(R.id.ListView01);
Intent intent = this.getIntent();
Bundle b = intent.getBundleExtra("data");
List list = new ArrayList();
list.add(b.getString("username"));
list.add(b.getString("password"));
list.add(b.getString("position"));
list.add(b.getString("gender"));
list.add(b.getString("hobby"));
list.add(b.getString("marriged"));
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
}
}
整個表單用 TableLayout 排版,靠 android:stretchColumns="1" 讓第二欄自動撐滿剩餘寬度,各列以 TableRow 分隔,這是早期做「欄位名稱 + 輸入元件」表單最省事的組合。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow android:id="@+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="用戶名稱" android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></EditText>
</TableRow>
<TableRow android:id="@+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="用戶密碼" android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:password="true"
></EditText>
</TableRow>
<TableRow android:id="@+id/TableRow03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="性別" android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<RadioGroup android:id="@+id/gender_g"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:text="男" android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></RadioButton>
<RadioButton android:text="女" android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
</TableRow>
<TableRow android:id="@+id/TableRow04"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="婚否" android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<ToggleButton android:text="@+id/ToggleButton01"
android:id="@+id/marriged"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ToggleButton>
</TableRow>
<TableRow android:id="@+id/TableRow05"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="愛好" android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<CheckBox android:text="閱讀" android:id="@+id/reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" ></CheckBox>
<CheckBox android:text="游泳" android:id="@+id/swimming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" ></CheckBox>
</TableRow>
<TableRow android:id="@+id/TableRow06"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="職務" android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<Spinner android:id="@+id/position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Spinner>
</TableRow>
<TableRow android:id="@+id/TableRow07"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="取消" android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="注冊" android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
這個範例把密碼明文放進 Bundle 傳遞,示範用途可以,正式專案絕對不可如此;密碼應立即雜湊處理,也不該出現在任何日誌或跨頁參數中。此外,資料在元件間傳遞時,用 Bundle 打包再經 Intent 投遞是延續早期 Android 的常見寫法,若資料量小也可直接用 Intent 的 putExtra 逐項傳遞。
Android 源碼中的觀察者模式:事件監聽模型解析
定義 Define a one-to-many dependency between objects so that when one object changes st
MediaPlayer 音訊與視訊播放:搭配 SurfaceView 與 SeekBar 的完整實作
前面的文章把 Android 介面的入門技術介紹得差不多了,相信讀者對常見的 Layout 與 View 都已經有概念。接下來不再談介面,改針對具體功能逐一深入。 本文
Android 性能優化實戰:緩存、數據結構與延遲加載
性能優化是 Android 開發中恆久的話題。除了佈局優化,代碼細節對程式運行效率的影響同樣不容小覷。本文從緩存、數據、延遲加載三個角度,梳理日常編碼中值得注意的性能要點
開啟 ListView 側邊快速滾動滑塊:一行屬性搞定 fastScroll
不少開發者不知道 ListView 右側那個快速滾動滑塊(fast scroll thumb)該怎麼啟用,其實只要一行設定。 用 XML 布局的話,在 ListView