Android 各控件的使用 - 复选框(CheckBox)

安卓复选框的使用

上一篇,这次是复选框CheckBox。

老规矩,添加Activity,src文件夹下添加一个CheckBoxActivity。

ExpandedBlockStart.gif View Code
package cn.Kurodo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public   class  CheckBoxActivity extends Activity {
    
private  CheckBox m_checkBox;
    
    
public  CheckBoxActivity() {
        
//  TODO Auto-generated constructor stub
    }

    @Override
    
protected   void  onCreate(Bundle savedInstanceState) {
        
//  TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox);
        
        m_checkBox 
=  (CheckBox)findViewById(R.id.checkbox);
        
        m_checkBox.setOnCheckedChangeListener(
new  CheckBoxListener());
        
        m_checkBox.setChecked(
true );  // 设置默认为勾选
    }
    
    
class  CheckBoxListener implements OnCheckedChangeListener {

        @Override
        
public   void  onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            
//  TODO Auto-generated method stub
            m_checkBox  =  (CheckBox)buttonView;
            Log.v(m_checkBox.toString() 
+   "  is " , String.valueOf(isChecked));
        }
    }
}

 

CheckBox的布局文件

 

ExpandedBlockStart.gif View Code
<? 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/checkbox"
    android:layout_width
="wrap_content"
    android:layout_height
="wrap_content"
    android:text
="@string/checkbox"   />
</ LinearLayout >

 

运行效果如下:

 

转载于:https://www.cnblogs.com/Kurodo/archive/2011/08/03/2126771.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值