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

(初识android) android组件 RadioButton,CheckBox,Toast简单介绍

2012-03-08 17:31 573 查看
RadioButton单选框,CheakBox多选框,Toast翻译过来就是:吐司,烤面包片[U],在android肯定不是这个意思啦,它有点像是一个对话框Dialog,但是又不同,它会在显示后过一段时间自动消失,给用户已提示作用。

1、RadioButton,CheakBox在布局中:

<RadioGroup
android:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<RadioButton
android:id="@+id/manrb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/manrb"
android:checked="true"
/>
<RadioButton
android:id="@+id/womanrb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/womanrb"

/>
</RadioGroup>

<CheckBox
android:id="@+id/swimckb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/swimckb"
android:layout_gravity="right"
/>
<CheckBox
android:id="@+id/readckb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/readckb"
/>
<CheckBox
android:id="@+id/runckb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/swimckb"
/>

从上我们可以看出,他们的大体使用和java并没有什么特别的变化,在使用RadioButton是必须是在一个RadioGroup中。

2.初始化

//初始化单选框
rg=(RadioGroup)findViewById(R.id.rg);
manrb=(RadioButton)findViewById(R.id.manrb);
womanrb=(RadioButton)findViewById(R.id.womanrb);

//初始化多选框
swimckb=(CheckBox)findViewById(R.id.swimckb);
readckb=(CheckBox)findViewById(R.id.readckb);
runckb=(CheckBox)findViewById(R.id.runckb);


3.在相应的类中首先进行初始化和其他安卓控件一样,但在添加监听事件时有所不同,在RadioGroup添加监听是实现RadioGroup 的OnCheckedChangeListener事件,而相对应的CheckBox没有相对应的OnCheckedChangeListener,是使用CompoundButton的OnCheckedChangeListener事件。 给单选框添加监听事件时,是对RadioGrup进行监听,然后就、根据选择(CheckId和你的RadioButton)相匹配,在执行相应的操作。

//给单选框添加监听事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(manrb.getId()==checkedId)
{
toast=Toast.makeText(RadioButtonActivity.this,"您选择的是男", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 50,-100);
toast.show();

}
else if(womanrb.getId()==checkedId)
{
toast=Toast.makeText(RadioButtonActivity.this,"您选择的是女", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 50,-100);
toast.show();

}
}
});

//给多选框添加监听
swimckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//执行操作
}
else{
//执行操作
}
}
});

readckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//执行操作
}
else{
//执行操作
}
}
});

runckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
//执行操作
}
else{
//执行操作
}
}
});


4.Toast的使用,它并不需要在布局文件进行布局,直接实例化一个Toast直接可使用。 具体使用:

toast=Toast.makeText(RadioButtonActivity.this,"您选择的是女", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 50,-100);
toast.show();


Toast具体说明:/article/5084332.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐