android设置单选按钮,RadioButton控件的使用(Android设置单选按钮)

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

android:id="@+id/sex"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/male"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="男" />

android:id="@+id/female"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="女"

android:layout_marginLeft="20dp"/>

上面我们已经讲过关于两种对于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){ //区分按钮

}

}

});