Android in the basic usage of the radio box RadioButton

To summarize setting icon in three ways:

(1) button attribute: mainly used for less demanding icon size, spacing requirements are not high occasion.

(2) background properties: mainly used in the case where an icon can be displayed in a larger space.

(3) drawableLeft properties: mainly used for requiring interval between the icons and text applications.

To set the android Note the use of background or drawableLeft when: button = "@ null"

monitor:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {


}
});
. 1
2
. 3
. 4
. 5
. 6
. 7
Here is an example:
renderings:

Layout file: activity_radio_button.xml use

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:button="@null"
android:checked="true"
android:drawablePadding="10dp"
android:drawableTop="@drawable/selback"
android:gravity="center_horizontal"
android:text="男"
android:textColor="#FF0033"
/>

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:button="@null"
android:drawablePadding="10dp"
android:drawableTop="@drawable/selback"
android:gravity="center_horizontal"
android:text="女"
android:textColor="#000000"
/>

</RadioGroup>

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
Selector: selback.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/on"/>
<item android:drawable="@mipmap/off"/>
</selector>
1
2
3
4
选择器用到的图片:

 

RadioButtonActivity the selected monitor

package rolechina.jremm.com.test4;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class RadioButtonActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton radioButton1;
private RadioButton radioButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);

radioGroup.setOnCheckedChangeListener (new new RadioGroup.OnCheckedChangeListener () {
@Override
public void OnCheckedChanged (RadioGroup Group, int checkedId) {
// display the selected text in red, black display is not selected
IF (radioButton1.isChecked ()) {
radioButton1.setTextColor (Color. parseColor ( "# FF0033"));
} the else {
radioButton1.setTextColor (Color.parseColor ( "# 000000"));
}

if(radioButton2.isChecked(http://www.my516.com)) {
radioButton2.setTextColor(Color.parseColor("#FF0033"));
}else{
radioButton2.setTextColor(Color.parseColor("#000000"));
}


}
});
}
}

--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/11069811.html