android组件之checkbox

checkbox不用我多解释也就是复选框,在web开发中该控件最常用的就是复选框的改变事件。在android中也是如此,这次还要和大家分享一下自己的一个小技巧来设置android组件的css样式。这个小技巧是这个样子的

1.先在values文件夹下面建一个xml文件文件的内容如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="mystyle">
     <item name="android:textColor">#00ff00</item>
     <item name="android:textSize">20px</item>
     <item name="android:autoLink">all</item>
     <item name="android:layout_width">fill_parent</item>
     <item name="android:layout_height">wrap_content</item>
     <item name="android:layout_marginTop">10px</item>
   </style>
</resources>

2.在组件的xml里只要加上这句就ok了

style="@style/mystyle"

mystyle是样式的名字


3.下面就是复选框的使用了,先来弄3个复选框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <CheckBox
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/checkbox1"
     android:text="上海"
  />
   <CheckBox
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/checkbox2"
     android:text="北京"
  />
   <CheckBox
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/checkbox3"
     android:text="广州"
  />
   <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/submit"
     android:text="确定"
   />
</LinearLayout>

下面则使用代码来演示一下,获取用户选中的复选框的值,思路是这样: 先获取到所有的被选中的复选框组件,然后判断组件是否是选中状态,如果是则的到复选框的值再添加到集合里面。

下面是主要的代码

package org.lxh.css;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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. */
   
    private List<CheckBox> checkbox=new ArrayList<CheckBox>();
    private Button but;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox);
        checkbox.add((CheckBox)this.findViewById(R.id.checkbox1));
        checkbox.add((CheckBox)this.findViewById(R.id.checkbox2));
        checkbox.add((CheckBox)this.findViewById(R.id.checkbox3));
        //设置第一个复选框为选中状态
        checkbox.get(0).setChecked(true);
        /*Iterator<CheckBox> it=checkbox.iterator();
        while(it.hasNext()){
        	CheckBox box=it.next();
        	box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
					//此处添加相应的事件
				}
			});
        }*/
        but=(Button)this.findViewById(R.id.submit);
        but.setOnClickListener(new View.OnClickListener() {
			
			
			public void onClick(View v) {
				 List<String> all=new ArrayList<String>();	
				 Iterator<CheckBox> it=checkbox.iterator();
				 while(it.hasNext()){
					 CheckBox box=it.next();
					 if(box.isChecked()){
						 all.add(box.getText().toString()); 
					 }
					
				 }
				 Toast.makeText(CheckboxActivity.this, all.toString(), 1).show();
			}
			
		});
        
        

      };

    
}

下面是运行效果图




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值