Android程序:checkbox的使用方法

来源:互联网 发布:网络教育包过 编辑:程序博客网 时间:2024/05/17 01:09
    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="唱歌"        android:id="@+id/checkBox"        />
public class MainActivity extends ActionBarActivity {    private CheckBox checkBox;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        checkBox = (CheckBox) findViewById(R.id.checkBox);        //通过设置checkbox的监听事件来对checkbox是不是被选中        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                //通过onCheckedChanged来监听当前的checkbox是否被选中                if(isChecked){                    //获得checkbox的文本内容                    String text = checkBox.getText().toString();                    Log.i("tag",text);                    Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();                }            }        });    }}
0 0
原创粉丝点击