編輯:關於 Android 程式設計
本文整理五種常見的選擇型控件實作方式,包含下拉列表、日期選擇器、時間選擇器、單項選擇與多項選擇,並附上布局與 Java 程式碼。
Spinner 是下拉列表的元件,透過 Adapter 綁定資料源,並以 OnItemSelectedListener 監聽選項變化。
activity_main.xml:
<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"
tools:context="com.rj141.sb.kongjian.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇您最喜歡的水果:"
android:textSize="18dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Spinner s;
private TextView tv;
String[] data = new String[]{"蘋果", "雪梨", "西瓜", "葡萄", "橙子", "草莓"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.tv);
s = (Spinner) this.findViewById(R.id.spinner);
s.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String str = data[position];
tv.setText("最喜歡的水果是:" + str);
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
});
}
}
其中 android.R.layout.simple_list_item_1 是系統內建的下拉樣式,data 為資料源;setOnItemSelectedListener 則是下拉列表的監聽器。

透過 DatePickerDialog 跳出日曆對話框,並以 OnDateSetListener 回傳使用者選擇的年月日。注意月份參數從 0 起算,顯示時需加 1。
activity_date.xml:
<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"
tools:context="com.rj141.sb.kongjian.DateActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp" />
<Button
android:id="@+id/btndate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日曆" />
</LinearLayout>
DateActivity.java:
public class DateActivity extends ActionBarActivity {
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
btn = (Button) this.findViewById(R.id.btndate);
tv = (TextView) this.findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(DateActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
tv.setText("您的出生日期是:" + String.format("%d-%d-%d", year, monthOfYear + 1, dayOfMonth));
}
}, 2000, 1, 2).show();
}
});
}
}



TimePickerDialog 用於選擇時與分,最後一個參數 true 表示採用 24 小時制。
public class DateActivity extends ActionBarActivity {
private Button btn;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
btn = (Button) this.findViewById(R.id.btndate);
tv = (TextView) this.findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TimePickerDialog(DateActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
tv.setText(String.format("%d:%d", hourOfDay, minute));
}
// 參數 0,0 為初始時間,true 表示 24 小時制
}, 0, 0, true).show();
}
});
}
}



使用 RadioGroup 搭配多個 RadioButton 實現單選,點擊提交後判斷所選答案。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="2+3="
android:textSize="22dp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp">
<RadioButton android:id="@+id/rb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A.2" />
<RadioButton android:id="@+id/rb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B.3" />
<RadioButton android:id="@+id/rb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C.4" />
<RadioButton android:id="@+id/rb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D.5" />
</RadioGroup>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
public class SingChoose extends AppCompatActivity {
private Button btn;
private RadioButton rbD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sing_choose);
rbD = (RadioButton) this.findViewById(R.id.rb4);
btn = (Button) this.findViewById(R.id.submit);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rbD.isChecked()) {
Toast.makeText(SingChoose.this, "正確,請加五分", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SingChoose.this, "錯誤,請減五分", Toast.LENGTH_SHORT).show();
}
}
});
}
}



使用多個 CheckBox 並實作 OnCheckedChangeListener,即可在勾選變化時彙整所有被選取的項目。
public class CheckChoose extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private CheckBox cb1, cb2, cb3, cb4, cb5;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_choose);
tv = (TextView) this.findViewById(R.id.tv);
cb1 = (CheckBox) this.findViewById(R.id.cb1);
cb2 = (CheckBox) this.findViewById(R.id.cb2);
cb3 = (CheckBox) this.findViewById(R.id.cb3);
cb4 = (CheckBox) this.findViewById(R.id.cb4);
cb5 = (CheckBox) this.findViewById(R.id.cb5);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
cb5.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String str = "您喜歡:";
if (cb1.isChecked()) str += cb1.getText() + ",";
if (cb2.isChecked()) str += cb2.getText() + ",";
if (cb3.isChecked()) str += cb3.getText() + ",";
if (cb4.isChecked()) str += cb4.getText() + ",";
if (cb5.isChecked()) str += cb5.getText() + ",";
tv.setText(str);
}
}

HandlerThread 源碼分析:讓子線程複用成為可能
我們都知道,在 Android 中執行耗時操作時需要另開子線程,任務結束後線程會自動銷毀。想象一下:如果項目裡要頻繁執行耗時任務,每次都開線程、用完又銷毀,無疑會帶來可觀
Android 自動化構建之 Ant 多渠道打包實踐(上)
前言 Ant 是一款歷史悠久的自動化構建工具,Android 開發者可以透過它實現自動化構建,也能完成多渠道打包。關於 apk 打包的方式一般有 Ant、Python、G
Android 屬性動畫入門:ValueAnimator、ObjectAnimator 與組合動畫
在 Android 開發中,適當使用動畫可以提升應用整體質感。早期的動畫方案主要包含幀動畫與補間動畫,而 Android 3.0 之後推出的屬性動畫,則讓開發者能更靈活地
Android Studio 多渠道打包與 build.gradle 設定指南
在正式 App 開發中,常需要針對不同發行渠道產生不同版本。build.gradle 可協助管理簽名、版本、渠道變數與打包設定。 defaultConfig 與簽名 de