安卓学习-界面-ui-RadioButton CheckBox

RadioButton  CheckBox

下面例子演示了2个功能,一个是RadioButton选择时的事件,还有一个是Button按钮点击查看这2个控件的属性

XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
     >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="性别"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <RadioGroup
                android:id="@+id/radioGroup1"
                >

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

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

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="是否本地人"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定" />

    </TableLayout>

</RelativeLayout>

java代码

public class MainActivity extends Activity {
    
    Button btn;
    RadioGroup radioGroup1;
    CheckBox checkBox1;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn=(Button)findViewById(R.id.button1);
        radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
        checkBox1=(CheckBox)findViewById(R.id.checkBox1);
        
        //按钮点击事件
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int checkedId=radioGroup1.getCheckedRadioButtonId();
                if(checkedId>0){
                    RadioButton r=(RadioButton)findViewById(checkedId);
                    Toast.makeText(getApplicationContext(), r.getText(), Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(getApplicationContext(), ""+checkBox1.isChecked(), Toast.LENGTH_SHORT).show();
                
            }
        });
        
        //radioGruop的选择事件
        radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r=(RadioButton)findViewById(checkedId);
                Toast.makeText(getApplicationContext(), "你选择的是"+r.getText(), Toast.LENGTH_SHORT).show();
            }
        });
        
    }
}
原文地址:https://www.cnblogs.com/weijj/p/3946221.html