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>
style="@style/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(); } }); }; }