RadioButton控件的使用

举个例子,设置男女的RadioButton。

<RadioGroup                                  //先设置RadioGroup把一组Radio放在一起
    android:id="@+id/sex"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioButton                             //在里面加入Radio
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男" />
    <RadioButton
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"
        android:layout_marginLeft="20dp"/>
</RadioGroup>

上面我们已经讲过关于两种对于Button监听器的使用操作了,今天会再加一种,正好放在一起整理。

//对于button的点击监听
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "OK!", Toast.LENGTH_SHORT).show();
    }
});


//对于button的选择情况进行监听
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        
    }
});


//对于radio的button组的选择情况进行监听
btn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){ //区分按钮
            
        }
    }
});

猜你喜欢

转载自blog.csdn.net/qq_38367681/article/details/83154663