Android學習筆記之CheckBox

來源:互聯網
上載者:User

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

main.xml

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical" ></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/theme" /><br /> <CheckBox<br /> android:id="@+id/apple"<br /> android:text="@string/apple"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/><br /> <CheckBox<br /> android:id="@+id/banana"<br /> android:text="@string/banana"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/></p><p></LinearLayout>

RadioGroupActivity.java

package Android.Activity;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.CheckBox;<br />import android.widget.CompoundButton;<br />import android.widget.Toast;</p><p>public class RadioGroupActivity extends Activity {<br /> /** Called when the activity is first created. */<br />private CheckBox appleCheck = null;<br />private CheckBox bananaCheck = null;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> //通過控制項的ID來得到代表控制項的對象<br /> appleCheck = (CheckBox)findViewById(R.id.apple);<br /> bananaCheck = (CheckBox)findViewById(R.id.banana);<br /> //為RadioGroup設定監聽器,需要注意的是,這裡的監聽器和Button控制項的監聽器有所不同<br /> appleCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {</p><p>public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {<br />// TODO Auto-generated method stub<br />if(isChecked){<br />Toast.makeText(RadioGroupActivity.this,"恭喜你,還剩很多蘋果", Toast.LENGTH_LONG).show();<br />}<br />}<br />});</p><p> bananaCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {</p><p>public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {<br />// TODO Auto-generated method stub<br />if(isChecked){<br />Toast.makeText(RadioGroupActivity.this,"恭喜你,還剩很多香蕉", Toast.LENGTH_LONG).show();<br />}<br />}<br />});<br /> }<br />}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.