具有两个状态的按钮,已选中或未选中。当按下或点击按钮时,状态会自动更改。
直接继承至Button直接子类
CheckBoxRadioButtonSwitchSwitchCompatToggleButtSEO靠我on间接子类
AppCompatCheckBoxAppCompatRadioButton相比较Button而言多出了一个监听事件(接口)
当复合按钮的检查状态发生变化时调用。
实现方法:onCheckedChanged( CompoundButton buttonView,boolean isChecked)
buttonViSEO靠我ew 复合按钮视图的状态。isChecked buttonView的新状态。简单介绍几个常用的
isChecked() — 获取当前状态performClick() — 调用此视图的OnClicSEO靠我kListener(如果已定义)setChecked(boolean checked) — 更改这个按钮的状态setOnCheckedChangeListener(CompoundButton.OnCSEO靠我heckedChangeListener listener)
当这个按钮的检查状态发生变化时,注册一个回调toggle() — 将视图的状态更改为当前状态的逆(反向)复选框:可以选中SEO靠我或取消选中的特定类型的双状态按钮。
例:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:SEO靠我//schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parenSEO靠我t"android:layout_height="fill_parent"><CheckBox android:id="@+id/checkbox_meat"android:layout_width=SEO靠我"wrap_content"android:layout_height="wrap_content"android:text="@string/meat"android:onClick="onChecSEO靠我kboxClicked"/><CheckBox android:id="@+id/checkbox_cheese"android:layout_width="wrap_content"android:SEO靠我layout_height="wrap_content"android:text="@string/cheese"android:onClick="onCheckboxClicked"/> SEO靠我 </LinearLayout> public void onCheckboxClicked(View view) {// Is the view now checked?boolSEO靠我ean checked = ((CheckBox) view).isChecked();// Check which checkbox was clickedswitch(view.getId()) SEO靠我{case R.id.checkbox_meat:if (checked)// Put some meat on the sandwichelse// Remove the meatbreak;casSEO靠我e R.id.checkbox_cheese:if (checked)// Cheese meelse// Im lactose intolerantbreak;// TODO: Veggie sanSEO靠我dwich} } public class MyActivity extends Activity {protected void onCreate(Bundle saSEO靠我vedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.content_layout_id);finSEO靠我al CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);if (checkBox.isChecked()) {checkBoxSEO靠我.setChecked(false);}}}注:AppCompatCheckBox作为其子类用法差别不大!
单选按钮:是可以选中或取消选中的双状态按钮。当单选按钮被取消选中时,用户SEO靠我可以单击来选中它。
注:单选按钮通常与RadioGroup在一起使用。当多个单选按钮在RadioGroup内时,检查一个单选按钮将取消选中所有其他单选按钮。 <?xml version=SEO靠我"1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/androSEO靠我id"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="verticSEO靠我al"><RadioButton android:id="@+id/radio_pirates"android:layout_width="wrap_content"android:layout_heSEO靠我ight="wrap_content"android:text="@string/pirates"android:onClick="onRadioButtonClicked"/><RadioButtoSEO靠我n android:id="@+id/radio_ninjas"android:layout_width="wrap_content"android:layout_height="wrap_conteSEO靠我nt"android:text="@string/ninjas"android:onClick="onRadioButtonClicked"/> </RadioGroup> SEO靠我 public void onRadioButtonClicked(View view) {// Is the button now checked?boolean checked = ((RadSEO靠我ioButton) view).isChecked();// Check which radio button was clickedswitch(view.getId()) {case R.id.rSEO靠我adio_pirates:if (checked)// Pirates are the bestbreak;case R.id.radio_ninjas:if (checked)// Ninjas rSEO靠我ulebreak;} }注:AppCompatRadioButton作为其子类用法差别不大!
开关:是一个双状态切换开关小部件,可以在两个选项之间进行选择。用户可以来回拖动“SEO靠我拇指”来选择所选择的选项,或者只需轻按以切换,就像复选框一样。该text 属性控制交换机标签中显示的文本,而 文本off和on文本控制拇指上的文本。
xml属性公共方法作用效果android:showTSEO靠我extsetShowText(boolean)是否显示 打开/关闭 文本android:textOffsetTextOff(CharSequence)当开关处于 关闭 状态时使用的文本android:SEO靠我textOnsetTextOn(CharSequence)当开关在 开打 状态时使用的文本android:tracksetTrackResource(int)开关拇指滑动的“轨迹”显示 打开/关闭 的状态的按钮,默认情况下伴随文本“ON”或“OFF”。
与Switch差别不大!
知识不够用?
http://android.xsoftlab.net/reference/androiSEO靠我d/widget/CompoundButton.html
网站备案号:浙ICP备17034767号-2