Android學習筆記(3)——基本控制項三

來源:互聯網
上載者:User

RadioGroup與RadioButton 控制項
 
首先需要在布局檔案中設定RadioGroup的屬性,然後在該RadioGroup中添加RadioButton的屬性。這也可以認為,RadioGroup是RadioButton的一個容器,首先建立容器,然後在容器中添加物體。
程式碼片段如下:
 
<RadioGroup 
    android:id="@+id/gender"//設定id
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"//設定RadioGroup中RadioButton的排列方向
>
    <RadioButton 
        android:id="@+id/male"//設定id
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/male"
    />
    <RadioButton 
        android:id="@+id/female"//設定id
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/female"
    />
</RadioGroup>
布局檔案寫完之後,在所在Activity類中添加RadioGroup和RadioButton的對象。
因為是單選的,所以這個控制項的事件需要設定在RadioGroup上,可以使用RadioGroup的setOnCheckedChangeListener的方法添加RadioGroup的OnCheckedChangeListener的監聽器,需要Override其中的onCheckedChanged(RadioGroup group, int checkedId)方法,參數很明顯,是所在group對象和所改變的RadioButton的id,在方法內部可以對RadioGroup以及RadioButton進行操作。
程式碼片段如下:
 
RadioGroup genderGroup = (RadioGroup)findViewById(R.id.gender);
RadioButton maleButton = (RadioButton)findViewById(R.id.male);
maleButton.setChecked(true);//預設選擇男
RadioButton femaleButton = (RadioButton)findViewById(R.id.female);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(femaleButton.getId()==checkedId){
                    Toast.makeText(ControlDemo2Activity.this, "Female", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(ControlDemo2Activity.this, "Male", Toast.LENGTH_SHORT).show();
                }
            }
        });
運行效果:
 

 
CheckBox控制項
 
首先要在布局檔案中定義CheckBox的樣式id等資訊,每一個CheckBox都需要定義。
程式碼片段如下:
 
<CheckBox 
    android:id="@+id/apple"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/apple"
/>
<CheckBox 
    android:id="@+id/orange"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/orange"
/>
<CheckBox 
    android:id="@+id/mango"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/mango"
/>
定義完CheckBox的之後,在Activity中建立CheckBox的對象,每個CheckBox上都可以設定監聽器,可以使用CheckBox的setOnCheckedChangeListener的方法添加CompoundButton的OnCheckedChangeListener的監聽器,需要Override其中的onCheckedChanged(CompoundButton arg0, boolean arg1),第一個參數是你所點擊的CheckBox的對象,第二個參數是該對象是否被選中的boolean值,在方法中可以自己定義一些功能效果。
 
程式碼片段如下:
 
CheckBox appleCheck = (CheckBox)findViewById(R.id.apple);
appleCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1==true)//當該Checkbox被按下
                    Toast.makeText(ControlDemo2Activity.this, "Apple Checked", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(ControlDemo2Activity.this, "Apple Unchecked", Toast.LENGTH_SHORT).show();
            }
        });
運行效果:

以上的兩種控制項的樣式是可以在布局檔案或者對象中可以設定。
附件為範例程式碼,僅供參考。嘎嘎~
 
本文出自 “戰神殿” 部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.