您的位置:首页 > 移动开发 > Android开发

Android控件之CheckBox、RadioButton

2015-11-19 14:42 387 查看
1、复选框CheckBox控件

public class CheckBox extend CompoundButton

复选框允许用户选择一个或多个选项。通常情况下,是从一个垂直列表中选择多个选项。每个复选框是分开管理的,必须为每个复选框注册监听事件。注册click监听事件的方式有两种。

xml布局文件如下:

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="23dp"
android:text="看小说" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:text="打网游" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:text="看电影" />


第一种:在使用该xml布局文件的Activity类实现CompoundButton.OnCheckedChangeListener接口

package com.example.checkboxradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {

private CheckBox cb1,cb2,cb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
cb2 = (CheckBox) this.findViewById(R.id.checkBox2);
cb3 = (CheckBox) this.findViewById(R.id.checkBox3);

cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

/**
* @param compoundButton 被单击的复选框
* @param isChecked 复选框选中为true,没有选中为false
*/
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
switch (compoundButton.getId()) {
case R.id.checkBox1:
if (isChecked) Toast.makeText(this, "选中看小说", Toast.LENGTH_SHORT).show();
else Toast.makeText(this, "取消看小说", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox2:
if (isChecked) Toast.makeText(this, "选中打网游", Toast.LENGTH_SHORT).show();
else Toast.makeText(this, "取消打网游", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox3:
if (isChecked) Toast.makeText(this, "选中看电影", Toast.LENGTH_SHORT).show();
else Toast.makeText(this, "取消看电影", Toast.LENGTH_SHORT).show();
break;
}

}

}


第二种:使用内部类的方式,对每一个复选框注入单击事件

cb1 = (CheckBox) this.findViewById(R.id.checkBox1);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

}
});


2、单选框RadioButton控件

单选按钮允许用户从一组中选择一个选项。由于单选按钮是相互排斥的,通过RadioGroup将多个RadioButton组合到一起,这样可以确保只能选择一个选项。

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox3"
android:layout_below="@+id/textView2" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="92dp"
android:layout_height="match_parent"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>


事件处理:onRadioButtonClicked。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: