在面板拖拽圖標到界面。然後更改相關屬性(Properties),如下圖:
activity_main.xml清單如下:
<CheckBox
Android:id="@+id/chkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioGroup01"
android:layout_alignParentRight="true"
android:text="@string/income2" />
<CheckBox
android:id="@+id/chkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/chkBox2"
android:layout_alignParentRight="true"
android:text="@string/income1" />
然後,我們開始編碼。
其相關部分的代碼如下:
- // CheckBox
- m_chkBox1 = (CheckBox) findViewById(R.id.chkBox1);
- m_chkBox2 = (CheckBox) findViewById(R.id.chkBox2);
-
- m_chkBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- String str = "You Choice: ";
- if (m_chkBox1.isChecked()) {
- str += m_chkBox1.getText();
- }
- setToastString(str).show();
- }
-
- });
-
- m_chkBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- String str = "You Choice: ";
- if (m_chkBox2.isChecked()) {
- str += m_chkBox2.getText();
- }
-
- setToastString(str).show();
- }
-
- });
- }
效果如下圖: