Android控制項開發之CheckBox

來源:互聯網
上載者:User

CheckBox,也就是多項選擇。Android中提供了ChechBox控制項,使用起來非常方便。


CheckBox效果

main.xml源碼

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/text"/>       <CheckBox       android:id="@+id/check1"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="蘋果 ios"  />       <CheckBox       android:id="@+id/check2"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="Google Android"  />       <CheckBox       android:id="@+id/check3"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="RIM BlackBerry"  />   <CheckBox       android:id="@+id/check4"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="微軟 Windows phone 7"  />       <CheckBox       android:id="@+id/check5"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="諾基亞 symbian"  />       <Button       android:id="@+id/mybutton"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="確定"  />   </LinearLayout>CheckBox 事件響應setOnCheckedChangeListener


本程式java源碼

import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast;public class CheckBoxActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)     {super.onCreate(savedInstanceState);setContentView(R.layout.main);                final CheckBox check1 = (CheckBox)findViewById(R.id.check1);final CheckBox check2 = (CheckBox)findViewById(R.id.check2);final CheckBox check3 = (CheckBox)findViewById(R.id.check3);final CheckBox check4 = (CheckBox)findViewById(R.id.check4);final CheckBox check5 = (CheckBox)findViewById(R.id.check5);              //建立CheckBox事件監聽器          check1.setOnCheckedChangeListener(listener);         check2.setOnCheckedChangeListener(listener);         check3.setOnCheckedChangeListener(listener);         check4.setOnCheckedChangeListener(listener);         check5.setOnCheckedChangeListener(listener);            }         private OnCheckedChangeListener listener = new OnCheckedChangeListener()    {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked){switch(buttonView.getId()){case R.id.check1:if(isChecked)Toast.makeText(getApplicationContext(), "你喜歡蘋果 ios智能手機系統", Toast.LENGTH_LONG).show();break;case R.id.check2:if(isChecked)Toast.makeText(getApplicationContext(), "你喜歡Google Android智能手機系統", Toast.LENGTH_LONG).show();break;case R.id.check3:if(isChecked)Toast.makeText(getApplicationContext(), "你喜歡RIM BlackBerry智能手機系統",Toast.LENGTH_LONG).show();break;case R.id.check4:if(isChecked)Toast.makeText(getApplicationContext(), "你喜歡微軟 Windows phone 7智能手機系統", Toast.LENGTH_LONG).show();break;case R.id.check5:if(isChecked)Toast.makeText(getApplicationContext(), "你喜歡諾基亞 symbian智能手機系統", Toast.LENGTH_LONG).show();break;}}         };

相關文章

聯繫我們

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