CheckBox介绍与应用

CheckBox一般用来提供给用户输入信息的组件,可以一次选择多个选项.这个组件解决了不是很方便的手机屏幕操作输入时.用选择组件供用户单击输入选项,显得非常有用.

在Android中,和其他组件一样,也是在xml中定义,安装前面讲解的其他组件的编程思路,

首先在main.xml文件中再增加一个按钮"CheckBox",单击该按钮程序运行将展示介绍CheckBox的界面.

<Button android:id = "@+id/check_box"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text = "Check Box"
/>
     
代码解释:

定义一个id为check_box的Button ,并设定其宽度和高度都是自适应,并且指定其显示的文字是"Check Box"

其对应的响应方法代码如下:

Button bt = (Button)findViewById(R.id.check_box);
bt.setOnClickListener(new OnClickListener()
{
    public void OnClick(View v)
    {
        Intent intent = new Intent(mainActivity.this,CheckBoxActivity.class);
        startActivity(intent);
    }
});
代码解释:

在单击按钮的时候,启动CheckBoxActivity,这个CheckBoxActivity就是用来展示CheckBox组件的.

修改CheckBoxActivity的onCreate方法,实现代码如下:

public void onCreate(savedInstanceState)
{
    super.onCreate(savedInstanceStete);
    setTitle("CheckBoxActivity");
    setContentView(R.layout.check_box);
}
代码解释:

上述代码是将其关联在check_box.xml的布局模板上,

然后需要在里面添加CheckBox组件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<CheckBox android:id = "@+id/plain_cb"
android:text = "Plain"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>
<CheckBox android:id = @+id/serif_cb"
andaroid:text = "Serif"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:typeface = "serif"
/>
<CheckBox android:id = "@+id/bold_cb"
android:text = "Bold"
android:layout_width ="wrap_content"
android:layout_height = "wrap_content"
android:textStyle = "bold"
/>
<CheckBox android:id = "@+id/italic_cb"
android:text = "Italic"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textStyle = "italic"
/>
</LinearLayout>
代码解释:

上述代码中实现添加4个CheckBox组件,并设定各自的宽度和高度都是自适应的,

然后需要考虑一下如何处理CheckBox的选中或者未选中,在这个应用中添加一个Button按钮,单击Button按钮时,程序运行获取4个Checkbox的状态,进而将被选中的Checkbox的值显示在Title 上.

首先在check_box.xml文件中添加一个Button按钮,实现代码如下:

<Button android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "显示Check Box的值"
/>
然后编写其对应的单击响应方法,实现代码如下所示:

Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener()
{
    OnClick(View v)
    {
        StringBuffer sb = "";
        if(plain_cb.isChecked())
        {
            sb = plain_cb.getText();
        }
        if(serif_cb.isChecked())
         {
             sb = sb + " , " + serif_cb.getText();
         } 
        if(italic_cb.isChecked())
         {
             sb = sb + " , " + italic_cb.getText();
         } 
        if(bold_cb.isChecked())
         {
             sb = sb + " , " + bold_cb.getText();
         } 
    setTitle("Checked: "+ sb);
    }
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值