編輯:關於 Android 開發
還不到睡覺時間,於是又加做了一個註冊畫面。延續上一篇登入頁的視覺風格,這次的新課題是下拉選單、標題欄處理,以及用 CheckBox 控制按鈕的啟用狀態。

1. 下拉列表(Spinner)的使用。
2. 標題欄的顯示與樣式問題。
3. 按鈕的 enable 狀態設定。
<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:focusable="true"
android:focusableInTouchMode="true"
android:background="#F7F7F9"
tools:context="com.dragon.android.qqregist.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:drawableLeft="@drawable/aa"
android:text="@string/button2"
android:textColor="#1CBAF5" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:background="#ffffff"
android:gravity="center"
android:text="@string/pagename"
android:textColor="#1CBAF5" />
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/button2"
android:paddingTop="30dp"
android:paddingBottom="20dp" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_spinner"
android:layout_weight="1"
android:entries="@array/country"/>
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/bg_edittext"
android:ems="10"
android:inputType="phone"
android:hint="@string/innum"
android:textSize="15sp" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@id/linear"
android:enabled="false"
android:background="@drawable/bg_button"
android:text="@string/button"
android:gravity="center"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@id/button1"
android:text="@string/sure"
android:textSize="12sp"
android:textColor="#A6A6A7" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkBox1"
android:autoLink="all"
android:text="@string/protocol"
android:textSize="12sp" />
</RelativeLayout>
幾個值得注意的設定:Spinner 與 EditText 都使用 layout_width="0dp" 搭配 layout_weight,這是 LinearLayout 中做比例分配的正確寫法;若寫成 wrap_content 再配 weight,寬度計算會不如預期。
登入鈕在 XML 中預設 android:enabled="false",也就是初始狀態不可用,必須等使用者勾選同意條款後才開放——這正是問題 3 的線索。
EditText 背景 bg_edittext:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
<stroke android:width="1px" android:color="#BEBEBE"/>
<solid android:color="#FFFFFF" />
<padding
android:left="10dp"
android:top="10dp"
android:bottom="10dp"/>
</shape>
Spinner 背景 bg_spinner(內容與上者相同,靠程式或複本區隔):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
<stroke android:width="1px" android:color="#BEBEBE"/>
<solid android:color="#FFFFFF" />
<padding
android:left="10dp"
android:top="10dp"
android:bottom="10dp"/>
</shape>
按鈕的兩種狀態。bg_button 是啟用時的樣式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
<solid android:color="#1CBAF5"/>
<corners android:radius="10dp"/>
<padding
android:top="10dp"
android:bottom="10dp"/>
</shape>
bg_buttin_change 則是停用時的灰色樣式,兩者僅有 solid 的顏色不同。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
<solid android:color="#808080"/>
<corners android:radius="10dp"/>
<padding
android:top="10dp"
android:bottom="10dp"/>
</shape>
這裡有個更好的做法值得一提:與其準備兩個 shape 檔再於程式碼中切換,不如用 selector 搭配 state_enabled,讓系統依狀態自動選圖,程式碼完全不必介入。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="country">
<item>中國 +86</item>
<item>香港 +852</item>
<item>澳門 +853</item>
<item>台灣 +886</item>
<item>日本 +81</item>
<item>美國 +1</item>
<item>英國 +44</item>
</string-array>
</resources>
用 android:entries="@array/country" 就能直接把陣列綁進 Spinner,一行配置取代一整段 Adapter 程式碼。這在選項固定的情況下是最省事的做法。
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="https://schemas.android.com/apk/res/android">
<style name="bg_title" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/Titleground</item>
<item name="android:windowTitleStyle">@style/windowTitleStyle</item>
<item name="android:windowTitleSize">40dp</item>
</style>
<style name="Titleground">
<item name="android:background">#FFFFFF</item>
</style>
<style name="windowTitleStyle">
<item name="android:text">@string/pagename</item>
<item name="android:textColor">#1CBAF5</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
原本想透過 windowTitleBackgroundStyle 與 windowTitleStyle 直接改寫系統標題欄,但實際套用後效果不如預期,於是中途放棄。
替代方案:直接隱藏標題欄。在 AndroidManifest.xml 的 activity 標籤中加入:
android:theme="@android:style/Theme.NoTitleBar"
然後在版面中自繪一個標題列——也就是配置檔中那個「返回鈕 + 置中標題」的組合。這個決定其實是對的:系統標題欄的可客製程度向來有限,自繪標題列(或直接用 Toolbar)才能自由控制高度、字型與互動。
package com.dragon.android.qqregist;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Spinner spinner = null;
private EditText editText1;
private Button button2;
private Button button1;
private CheckBox checkBox1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setSelection(0);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.setHintTextColor(Color.GRAY);
button2 = (Button) findViewById(R.id.button2);
// 讓返回鈕置頂顯示
button2.bringToFront();
button1 = (Button) findViewById(R.id.button1);
// Spinner 選擇監聽
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
// parent 為當前 Spinner、pos 為選中項的位置
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String[] country = getResources().getStringArray(R.array.country);
Toast.makeText(MainActivity.this, "你選擇的是:" + country[pos],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// 沒有任何選項被選取時的回呼
}
});
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
@SuppressLint("NewApi")
public void onCheckedChanged(CompoundButton view, boolean inChecked) {
// 勾選與否直接決定按鈕是否可用
button1.setEnabled(inChecked);
button1.setBackground(getResources().getDrawable(
inChecked ? R.drawable.bg_button : R.drawable.bg_button_change));
}
});
}
}
setEnabled 已經會換外觀。程式碼中同時呼叫 setEnabled() 與 setBackground(),其實前者本身就會讓 Button 進入 disabled 狀態;只要改用 selector 定義 state_enabled 的兩種樣式,這段 if-else 可以整段刪掉。
setHintTextColor 建議寫在 XML。用 android:textColorHint="#A6A6A7" 就能達到同樣效果,不必在程式碼中呼叫。
getDrawable() 已棄用。新版 API 建議改用 ContextCompat.getDrawable(this, R.drawable.bg_button),可以避開部分型別轉換的相容問題。
缺少真正的表單驗證。勾選條款只代表使用者同意了協議,不代表手機號碼合法。正式實作還應在按下按鈕時檢查號碼格式與驗證碼,並在送出期間鎖定按鈕避免重複提交。
aa.png
Android 廣播機制 BroadcastReceiver 完整入門
Android 系統內部定義了大量廣播:電池狀態改變、來電、簡訊抵達、開機完成,都會送出對應的廣播訊息。當然,開發者也可以自行定義專屬的廣播。 有廣播就一定有發送者與接收
Android 界面架構剖析:Activity、PhoneWindow 與 DecorView
Android 界面架構剖析:Activity、PhoneWindow 與 DecorView在一個 Android 應用中,用戶界面由 View 與 ViewGroup
React Native for Android 環境建置與第一個專案
前言React Native 讓開發者能用 JavaScript 與 React 建立跨平台應用。本文說明如何在 Android 環境中完成基礎設定,並建立第一個 Rea
ListView 多類型 Item 實現方案與 RecycleBin 回收機制源碼剖析
一、概述 一般情況下,ListView 的每個 item 樣式都相同,但實際場景中不同位置的 item 需要不同樣式的需求非常多。 以微信為例:早期版本的代表作是消息列表