Android控件开发之CheckBox

来源:互联网 发布:如何复制淘宝上的图片 编辑:程序博客网 时间:2024/06/05 07:47

CheckBox,也就是多项选择。Android中提供了ChechBox控件,使用起来非常方便。


CheckBox效果



main.xml源码

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView       
  8.     android:layout_width="fill_parent"      
  9.     android:layout_height="wrap_content"      
  10.     android:text="@string/text"/>    
  11.        
  12. <CheckBox     
  13.     android:id="@+id/check1"    
  14.     android:layout_width="fill_parent"    
  15.     android:layout_height="wrap_content"    
  16.     android:text="苹果 ios"  />     
  17.       
  18. <CheckBox     
  19.     android:id="@+id/check2"    
  20.     android:layout_width="fill_parent"    
  21.     android:layout_height="wrap_content"    
  22.     android:text="谷歌 Android"  />     
  23.       
  24. <CheckBox     
  25.     android:id="@+id/check3"    
  26.     android:layout_width="fill_parent"    
  27.     android:layout_height="wrap_content"    
  28.     android:text="RIM BlackBerry"  />    
  29.    
  30. <CheckBox     
  31.     android:id="@+id/check4"    
  32.     android:layout_width="fill_parent"    
  33.     android:layout_height="wrap_content"    
  34.     android:text="微软 Windows phone 7"  />    
  35.        
  36. <CheckBox     
  37.     android:id="@+id/check5"    
  38.     android:layout_width="fill_parent"    
  39.     android:layout_height="wrap_content"    
  40.     android:text="诺基亚 symbian"  />    
  41.        
  42. <Button     
  43.     android:id="@+id/mybutton"    
  44.     android:layout_width="fill_parent"    
  45.     android:layout_height="wrap_content"    
  46.     android:text="确定"  />     
  47.   
  48. </LinearLayout>  
  49.   
  50. CheckBox 事件响应setOnCheckedChangeListener  


本程序java源码

[html] view plaincopy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.widget.CheckBox;  
  4. import android.widget.CompoundButton;  
  5. import android.widget.CompoundButton.OnCheckedChangeListener;  
  6. import android.widget.Toast;  
  7.   
  8. public class CheckBoxActivity extends Activity   
  9. {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState)   
  13.     {  
  14.     super.onCreate(savedInstanceState);  
  15.     setContentView(R.layout.main);  
  16.                   
  17.     final CheckBox check1 = (CheckBox)findViewById(R.id.check1);  
  18.     final CheckBox check2 = (CheckBox)findViewById(R.id.check2);  
  19.     final CheckBox check3 = (CheckBox)findViewById(R.id.check3);  
  20.     final CheckBox check4 = (CheckBox)findViewById(R.id.check4);  
  21.     final CheckBox check5 = (CheckBox)findViewById(R.id.check5);  
  22.           
  23.         //创建CheckBox事件监听器  
  24.           check1.setOnCheckedChangeListener(listener);  
  25.          check2.setOnCheckedChangeListener(listener);  
  26.          check3.setOnCheckedChangeListener(listener);  
  27.          check4.setOnCheckedChangeListener(listener);  
  28.          check5.setOnCheckedChangeListener(listener);  
  29.           
  30.     }  
  31.        
  32.     private OnCheckedChangeListener listener = new OnCheckedChangeListener()  
  33.     {  
  34.     @Override  
  35.     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)  
  36.     {  
  37.         switch(buttonView.getId())  
  38.         {  
  39.         case R.id.check1:  
  40.             if(isChecked)  
  41.                 Toast.makeText(getApplicationContext(), "你喜欢苹果 ios智能手机系统", Toast.LENGTH_LONG).show();  
  42.             break;  
  43.         case R.id.check2:  
  44.             if(isChecked)  
  45.                 Toast.makeText(getApplicationContext(), "你喜欢谷歌 Android智能手机系统", Toast.LENGTH_LONG).show();  
  46.             break;  
  47.         case R.id.check3:  
  48.             if(isChecked)  
  49.                 Toast.makeText(getApplicationContext(), "你喜欢RIM BlackBerry智能手机系统",Toast.LENGTH_LONG).show();  
  50.             break;  
  51.         case R.id.check4:  
  52.             if(isChecked)  
  53.                 Toast.makeText(getApplicationContext(), "你喜欢微软 Windows phone 7智能手机系统", Toast.LENGTH_LONG).show();  
  54.             break;  
  55.         case R.id.check5:  
  56.             if(isChecked)  
  57.                 Toast.makeText(getApplicationContext(), "你喜欢诺基亚 symbian智能手机系统", Toast.LENGTH_LONG).show();  
  58.             break;  
  59.             }  
  60.         }              
  61.     };  
0 0