androi_checkbox多选框学习

本文介绍了一个基于Android平台的复选框布局文件及MainActivity.java的实现方式。该应用通过多个CheckBox组件允许用户选择不同的编程语言,并实现了全选、反选的功能。此外,还提供了获取已选中选项及自定义添加复选框的功能。
<?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"
    >

   <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
   >
      <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/allChoice"
        android:id="@+id/allChoiceCheckBox"
      />
      <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/otherChoice"
        android:layout_toRightOf="@id/allChoiceCheckBox"
        android:layout_alignTop="@id/allChoiceCheckBox"
        android:id="@+id/otherChoiceCheckBox"
      />

   </RelativeLayout>
   
    <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
   >
      <CheckBox
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/java"
        android:id="@+id/javaCheckBox"
      />
      <CheckBox
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/php"
        android:layout_toRightOf="@id/javaCheckBox"
        android:layout_alignTop="@id/javaCheckBox"
        android:id="@+id/phpCheckBox"
      />

   </RelativeLayout>
   
    <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
   >
      <CheckBox
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/android"
        android:id="@+id/androidCheckBox"
      />
      <CheckBox
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/oracle"
        android:layout_toRightOf="@id/androidCheckBox"
        android:layout_alignTop="@id/androidCheckBox"
        android:id="@+id/oracleCheckBox"
      />
   </RelativeLayout>
   
    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/addCheckBox"
    >
     </RelativeLayout>
   
    <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:gravity="center"
     android:paddingTop="20px"
   >
      <Button
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/getValue"
        android:id="@+id/getValueButton"
      />
      <Button
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/addDiy"
        android:layout_toRightOf="@id/getValueButton"
        android:layout_alignTop="@id/getValueButton"
        android:id="@+id/addDiyButton"
      />

   </RelativeLayout>


</LinearLayout>

上面是布局文件

下面是MainActivity.java

package com.android.checkbox.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
   
	private CheckBox allChoiceCheckBox;
	
	private CheckBox otherChoiceCheckBox;
	
	private CheckBox javaCheckBox;
	
	private CheckBox phpCheckBox;
	
	private CheckBox androidCheckBox;
	
	private CheckBox oracleCheckBox;
	
	private Button getValueButton;
	
	private Button addDiyButton;
	
	private ArrayList<CheckBox> checkBoxList=new ArrayList<CheckBox>();
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        allChoiceCheckBox=(CheckBox) this.findViewById(R.id.allChoiceCheckBox);
        otherChoiceCheckBox=(CheckBox) this.findViewById(R.id.otherChoiceCheckBox);
        
        javaCheckBox=(CheckBox) this.findViewById(R.id.javaCheckBox);
        phpCheckBox=(CheckBox) this.findViewById(R.id.phpCheckBox);
        androidCheckBox=(CheckBox) this.findViewById(R.id.androidCheckBox);
        oracleCheckBox=(CheckBox) this.findViewById(R.id.oracleCheckBox);
        checkBoxList.add(javaCheckBox);
        checkBoxList.add(phpCheckBox);
        checkBoxList.add(androidCheckBox);
        checkBoxList.add(oracleCheckBox);
        
        getValueButton=(Button) this.findViewById(R.id.getValueButton);
        
        addDiyButton=(Button) this.findViewById(R.id.addDiyButton);
        
        // 全选
         allChoiceCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			     for( CheckBox checkBox : checkBoxList)
		    	    {
		    	    	checkBox.setChecked(isChecked);
		    	    }
			}
		});
         //反选
         otherChoiceCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		    	    for( CheckBox checkBox : checkBoxList)
		    	    {
		    	    	checkBox.setChecked(!checkBox.isChecked());
		    	    	
		    	    }
		    	    //allChoiceCheckBox.setChecked(false);
			}
		});
         
         getValueButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				  StringBuffer sb=new StringBuffer();
				    for( CheckBox checkBox : checkBoxList)
		    	    {
		    	    	 if(checkBox.isChecked())
		    	    	 {
		    	    		 sb.append(checkBox.getText()).append(",");
		    	    	 }
		    	    	
		    	    }
				    sb.deleteCharAt(sb.length()-1);
				    Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
			}
		});
         
         addDiyButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				RelativeLayout addCheckBox=(RelativeLayout) MainActivity.this.findViewById(R.id.addCheckBox);
			     CheckBox cb=new CheckBox(MainActivity.this);
			   //  LayoutParams  params=new LayoutParams();
			    // cb.setLayoutParams(params)
				//addCheckBox.addView(child, params)
			}
		});
    }
}


转载于:https://my.oschina.net/jgy/blog/51226

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值