Android学习-常见的UI控件 RadioGroup和RadioButton

RadioGroup和RadioButton

1.RadioGroup
RadioButton的一个集合,提供多选一机制
2.属性
android:orientation=”vertical”–垂直排布 “horizontal”–水平排布
决定当前RadioGroup中RadioButton以什么形式排列

id="@+id/radiogroup"
        android:orientation="horizontal"
        android:layout_width="224dp"
        android:layout_height="153dp" >

        id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    
public class FirstActivity extends AppCompatActivity {

    private RadioGroup rg;
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //将布局xml文件引入到activity中来
        setContentView(R.layout.first_layout);

        rg = (RadioGroup) findViewById(R.id.radiogroup);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup,  int i) {
                switch (i){
                    case R.id.radioButton:
                        System.out.println("1111111111");
                        break;
                    case R.id.radioButton2:
                        Log.d("tag", "onCheckedChanged: 22222222222");
                        break;
                }
            }
        });
    }

}

你可能感兴趣的:(Android)