Android學習筆記之CheckBox

來源:互聯網
上載者:User

CheckBox複選按鈕是一種有雙狀態按鈕的特殊類型,可以選中或者不選中。可以現在布局檔案中定義多選按鈕,然後對每一個多選按鈕進行事件監setOnCheckedChangeListener,通過isChecked來判斷選項是否被選中


 
main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/theme" /> 
    <CheckBox  
        android:id="@+id/apple" 
        android:text="@string/apple" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/> 
    <CheckBox 
        android:id="@+id/banana" 
        android:text="@string/banana" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/> 
         
 
</LinearLayout> 
 
RadioGroupActivity.java
 
package Android.Activity; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.Toast; 
 
 
public class RadioGroupActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private CheckBox appleCheck = null; 
    private CheckBox bananaCheck = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        //通過控制項的ID來得到代表控制項的對象 
        appleCheck = (CheckBox)findViewById(R.id.apple); 
        bananaCheck = (CheckBox)findViewById(R.id.banana); 
        //為RadioGroup設定監聽器,需要注意的是,這裡的監聽器和Button控制項的監聽器有所不同 
        appleCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
             
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
                // TODO Auto-generated method stub 
                if(isChecked){ 
                    Toast.makeText(RadioGroupActivity.this,"恭喜你,還剩很多蘋果", Toast.LENGTH_LONG).show(); 
                } 
            } 
        });  www.2cto.com
         
        bananaCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
             
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
                // TODO Auto-generated method stub 
                if(isChecked){ 
                    Toast.makeText(RadioGroupActivity.this,"恭喜你,還剩很多香蕉", Toast.LENGTH_LONG).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.