編輯:關於 Android 程式設計
今天項目中用到了一些 Android 常見控件——RadioGroup、RadioButton、Spinner 和 EditText,藉此機會整理一下它們的用法,希望能對需要的朋友有所幫助。
RadioButton 就是我們常見的單選按鈕,一個 RadioGroup 可以容納多個單選按鈕,但每次只能選中其中一個。先看布局:
<RadioGroup
android:id="@+id/quickResponseRG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/yesRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ignore_yes"
android:textSize="22sp" />
<RadioButton
android:id="@+id/noRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ignore_no"
android:textSize="22sp" />
</RadioGroup>
要監聽選中變化,需實作 RadioGroup.OnCheckedChangeListener 介面,覆寫 onCheckedChanged(RadioGroup group, int checkedId)。參數 group 是添加監聽器的 RadioGroup,checkedId 則是新選中 RadioButton 的 ID(清空選擇時為 -1)。
Spinner 是下拉列表選擇框,可以綁定資料來源——資料來源可以在程式碼中定義,也可以在 values/strings 中以字串陣列定義,還能從資料庫查詢取得。綁定資料時用 ArrayAdapter:
String[] intervalTime = getResources().getStringArray(R.array.intervalTime);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, intervalTime);
bstIntervalSpinner.setAdapter(adapter);
監聽選擇則需實作 AdapterView.OnItemSelectedListener,重點覆寫 onItemSelected(AdapterView parent, View view, int position, long id),透過 position 就能取得選中項。
EditText 用來輸入文字,為它添加監聽器後,可以即時監測輸入內容,例如檢查是否有誤、是否符合規範、長度是否超標。透過 addTextChangedListener 掛上實現了 TextWatcher 介面的監聽器:
powerEditText.addTextChangedListener(editTextListener);
TextWatcher 提供三個方法可按需覆寫:beforeTextChanged(文字將改變前)、onTextChanged(文字正在改變)、afterTextChanged(文字已改變)。通常在 afterTextChanged 中讀取最終輸入內容最為合適。
先看效果圖,圖中有 3 個 RadioGroup、2 個 Spinner 和 1 個 EditText。由於程式設定了監聽器,上方選項的任何變化都會即時反映到下方分割線以下的結果區:
Java 主要程式碼如下:
public class InitActivity extends Activity {
private RadioGroup quickResponseRadioGroup;
private RadioGroup channelNumRadioGroup;
private RadioGroup crcCkeckRadioGroup;
private Spinner bstIntervalSpinner;
private Spinner transIntervalSpinner;
private EditText powerEditText;
private String[] intervalTime;
private TextView resultTextView1, resultTextView2, resultTextView3,
resultTextView4, resultTextView5, resultTextView6;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.init_activity);
// findViewById 初始化各控件(略)
quickResponseRadioGroup.setOnCheckedChangeListener(new RadioGroupListener(quickResponseRadioGroup));
channelNumRadioGroup.setOnCheckedChangeListener(new RadioGroupListener(channelNumRadioGroup));
crcCkeckRadioGroup.setOnCheckedChangeListener(new RadioGroupListener(crcCkeckRadioGroup));
powerEditText.addTextChangedListener(new EditTextListener());
intervalTime = getResources().getStringArray(R.array.intervalTime);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, intervalTime);
bstIntervalSpinner.setAdapter(adapter);
transIntervalSpinner.setAdapter(adapter);
bstIntervalSpinner.setOnItemSelectedListener(new SpinnerListener(bstIntervalSpinner));
transIntervalSpinner.setOnItemSelectedListener(new SpinnerListener(transIntervalSpinner));
}
// 監聽 RadioGroup
class RadioGroupListener implements RadioGroup.OnCheckedChangeListener {
private RadioGroup myRadioGroup;
public RadioGroupListener(RadioGroup radioGroup) {
this.myRadioGroup = radioGroup;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (myRadioGroup.getId()) {
case R.id.quickResponseRG:
resultTextView1.setText(checkedId == R.id.yesRadioButton ? "00" : "01");
break;
case R.id.channelNumRG:
resultTextView5.setText(((RadioButton) findViewById(checkedId)).getText().toString());
break;
case R.id.crcCheckRG:
resultTextView6.setText(((RadioButton) findViewById(checkedId)).getText().toString());
break;
default:
break;
}
}
}
// 監聽 EditText
class EditTextListener implements TextWatcher {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override public void afterTextChanged(Editable s) {
resultTextView4.setText(powerEditText.getText().toString().trim());
}
}
// 監聽 Spinner
class SpinnerListener implements AdapterView.OnItemSelectedListener {
private Spinner mySpinner;
public SpinnerListener(Spinner spinner) {
this.mySpinner = spinner;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mySpinner.getId() == R.id.BSTintervalSpinner) {
resultTextView2.setText(intervalTime[position]);
} else if (mySpinner.getId() == R.id.transRetrySpinner) {
resultTextView3.setText(intervalTime[position]);
}
}
@Override public void onNothingSelected(AdapterView<?> parent) { }
}
}
總結一下:RadioGroup 搭配 OnCheckedChangeListener 處理單選、Spinner 搭配 OnItemSelectedListener 處理下拉選擇、EditText 搭配 TextWatcher 即時監聽輸入。這幾個控件幾乎覆蓋了日常表單交互的大多數場景,把它們的監聽機制摸熟,界面與資料的雙向同步便可游刃有餘。
透視 Android 系統架構與記憶體管理優化
要把 App 寫好、寫穩,理解底下這層系統怎麼運作會有決定性的幫助。本文從 Android 的整體分層講起,一路談到 Dalvik 虛擬機的設計取捨、Low Memory
Android 項目部署之 Nexus 私服搭建與應用
一、概述 Nexus 是一個基於 Maven 的倉庫管理社區項目,最典型的用場就是在局域網搭建一個 Maven 私服,用來部署第三方公共構件,或作為遠程倉庫在該局域網的代
Android 自訂 Dialog 三步驟:樣式客製與參數雙向傳遞
前面幾篇都在用系統現成的 API 拼對話框,但系統函式的彈性實在有限——一旦設計稿要求的版面不是「標題+訊息+兩個按鈕」,就只能自己來。本文完整走一遍自訂 Dialog:
Android 逐幀動畫播放的實作步驟
接下來會陸續整理一些 Android 開發中常用的實用技巧,這一篇先從最基礎的動畫播放談起。 做法總覽 Android 的逐幀動畫本質上就是「按順序快速切換多張圖片」,