Android之CheckBox复选框控件使用inelayout.xml Xml代码

本文介绍了如何在Android应用中动态加载并使用CheckBox控件。通过MainActivity.java代码示例展示了如何创建CheckBox列表,并在用户点击按钮后显示所选选项。在布局文件linelayout.xml和checkbox.xml中定义了CheckBox的基本结构。
摘要由CSDN通过智能技术生成

linelayout.xml

Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <Button android:layout_width="fill_parent" android:layout_height="wrap_content"  
  7.         android:id="@+id/buton" android:text="@string/button">  
  8.     </Button>  
  9. </LinearLayout>  

checkbox.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" >  
  5. </CheckBox>  

 MainActivity.java

Java代码   收藏代码
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.CheckBox;  
  11. import android.widget.LinearLayout;  
  12.   
  13. public class MainActivity extends Activity  implements OnClickListener{  
  14.   
  15.     private List <CheckBox> checkboxs = new ArrayList<CheckBox> ();   
  16.       
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         //setContentView(R.layout.activity_main);  
  20.         String[] chekcBoxTexts = new String[]{"旅游","读书","看书","爬山","踢足球","打篮球","打乒乓球"};  
  21.         //动态加载布局  
  22.         LinearLayout linerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.linelayout, null) ;  
  23.         for(int i=0;i< chekcBoxTexts.length;i++){  
  24.             String item = chekcBoxTexts[i];  
  25.             //动态创建布局  
  26.             CheckBox checkbox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);  
  27.             checkbox.setText(item);  
  28.             checkboxs.add(checkbox);  
  29.               
  30.             linerLayout.addView(checkbox, i);  
  31.         }  
  32.         setContentView(linerLayout);  
  33.           
  34.         Button button = (Button) this.findViewById(R.id.buton);  
  35.         button.setOnClickListener(this) ;  
  36.     }  
  37.   
  38.     @Override  
  39.     public void onClick(View v) {  
  40.         StringBuffer s = new StringBuffer();  
  41.         for(CheckBox box : checkboxs){  
  42.             if(box.isChecked()){  
  43.                 if(s.length()>0){s.append("\n");}  
  44.                 s.append(box.getText()) ;  
  45.             }  
  46.         }  
  47.         if(s.length()==0){  
  48.             s.append("您还没有选择呢");  
  49.         }  
  50.         //使用提示框提示用户信息  
  51.         new AlertDialog.Builder(this).setMessage(s.toString()).setPositiveButton("关闭"null).show() ;  
  52.           
  53.     }  
  54. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值