经常使用基础空组件html
6 CheckBoxjava
复选框是经常使用组件之一,可是开发过程当中虽然不少地方会用到复选框,可是却不会用Android系统提供的原始样式,相似咱们在写html时,不一样的浏览器提供的复选框按钮是不同的,兼容性也不好,通常用图片替代假装。可是不管如何假装样式,复选框的功能都是同样的。android
layout中组件:浏览器
<CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="basketball"/> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="baseball"/>
Activity中组件:ide
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取checkbox cb1 = (CheckBox)findViewById(R.id.cb1); cb2 = (CheckBox)findViewById(R.id.cb2); // 为checkbox注册事件监听(当复选框被选中或从选中到未被选中状态时这个类的onCheckedChanged方法被触发) CBCheckedImpl impl = new CBCheckedImpl(); cb1.setOnCheckedChangeListener(impl); cb2.setOnCheckedChangeListener(impl); } // Activity内部类,实现android.widget.CompoundButton.OnCheckedChangeListener接口同时实现onCheckedChanged方法 btn表示发生状态变化的组件,flag若是为true表示选中,不然表示为被选中 @Override public void onCheckedChanged(CompoundButton btn, boolean flag) { // TODO Auto-generated method stub String rs = "nothing"; rs = flag?"选中了"+btn.getText().toString():"未选中"+btn.getText().toString(); Toast.makeText(CheckBoxActivity.this,rs, Toast.LENGTH_SHORT).show(); } }
注意:学习
1)复选框的使用基本和单选按钮差很少,主要就是要记住单选按钮实现的接口监听是android.widget.RadioGroup.OnCheckedChangeListener;然而复习按钮实现的监听是android.widget.CompoundButton.OnCheckedChangeListener。不要觉得单选按钮和复选按钮都是CompoundButton的子类,就应该实现相同的接口,其实是由于,单选按钮有组的概念,然而复选按钮没有组的概念。this
下一节将学习UI的spa
7 ProgressBarcode
8 ListView ListActivity SimpleAdapterorm