手记

复选框CheckBox概述与运用

CheckBox有两种状态:
选中(ture);未选中(false)

属性:
android:checked = "false" ----未选中
android:text = "男"

package marqueetext.marquee.iii.myapplication;
import android.util.Log;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {
    private CheckBox cb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cb = (CheckBox) findViewById(R.id.checkBox);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i("tag",isChecked+"" );
                if (isChecked){
                    String text = cb.getText().toString();//获取文本内容
                    Log.i("tag",text);  //输出文本内容
                }
            }
        });
    }
}
0人推荐
随时随地看视频
慕课网APP