package cn.edu.checkbox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class CheckBoxActivity extends Activity {
/** Called when the activity is first created. */
private CheckBox check1;
private CheckBox check2;
private CheckBox check3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
check1=(CheckBox)this.findViewById(R.id.check1);
check2=(CheckBox)this.findViewById(R.id.check2);
check3=(CheckBox)this.findViewById(R.id.check3);
check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
System.out.println(isChecked);
}
});
}
}
布局文件中
<CheckBox android:id="@+id/check1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
本文提供了一个 Android 应用中使用 CheckBox 的实例代码。该示例展示了如何创建并设置多个 CheckBox,并为其中一个 CheckBox 添加了状态改变监听器,以实现状态变化时打印状态的功能。
651

被折叠的 条评论
为什么被折叠?



