单选按钮&复选按钮

来源:互联网 发布:js设置div滚动条位置 编辑:程序博客网 时间:2024/04/30 20:30
复选按钮:需要通过按钮实现
onClick里面设置当checkbox.isChecked()默认为true就是选中状态;
xml标签:<CheckBox
        android:id="@+id/fuxuan0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定" />

java:public void dianji0(View v) {
            if (checkBox.isChecked()) {
            mTextView2.setText("被勾选");
            }else {
            mTextView2.setText("未勾选");
        }
    }

checkBox0
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {
                            Toast.makeText(MainActivity.this, "听音乐", 1).show();
                        }

                    }
                });
****************************************************************************
单选按钮:
xml标签:<RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/myRadioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/myRadioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

java:绑定三个标签的id
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
        mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
        mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
 /**
         * 单选按钮
         */
        mRadioGroup1 .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
               switch (checkedId) {
                        case R.id.man_0:
                            sex = radioButton0.getText().toString();
                            break;
                        case R.id.woman_0:
                            sex = radioButton1.getText().toString();
                            break;
                        default:
                            break;
                        }
                }
            }
        });

0 0
原创粉丝点击