編輯:關於 Android 開發
當需要讓使用者在多個選項中擇一時,RadioGroup 搭配 RadioButton 是最直覺的選擇。同一個 RadioGroup 內的單選按鈕會互斥,選了一個,其他會自動取消。
<RadioGroup
android:id="@+id/group1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio3" />
</RadioGroup>
<TextView
android:text="你的選擇是:"
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
為 RadioGroup 註冊 OnCheckedChangeListener,透過 getCheckedRadioButtonId() 取得被選中的按鈕,再把它的文字顯示到 TextView 上:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView) findViewById(R.id.txtResult);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.group1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int radioButtonId = radioGroup.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(radioButtonId);
textView.setText(rb.getText());
}
});
}
這樣一來,每當切換選項,下方的文字就會即時更新為目前所選的單選項,是表單類界面的基礎用法。
DrawableView 第三方開源畫板:手寫、調色、縮放與存檔
元件能力概覽 DrawableView 是一個 Android 手寫板元件,開箱就能用。它支援:調整畫筆顏色、調整線條粗細、手勢縮放與拖曳畫布、復原上一筆,以及將手繪結果
Intent(三):向下一個 Activity 傳遞資料
Intent(三):向下一個 Activity 傳遞資料 要往下傳遞資料到下一個 Activity 其實很簡單,可以採用 putExtra() 方法的重載版本,把想傳遞的
Android 插件化基礎(4):動態啟動插件中的 Activity
Author:鄭海波-莫川 簡介 如何動態啟動插件中的 Activity?先來分析需要做好哪些準備: 1. 插件中 Activity 類的加載,也就是 ClassLoad
Android 反編譯與二次打包實戰全流程
Android 反編譯與二次打包實戰全流程 身為 Android 開發者,工作中難免要反編譯別人的 apk,主要目的還是為了取彼之長、補己之短。本文透過一個簡單的例子,講