码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-08-30 12:34:09      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   java   ar   2014   

RadioButton  CheckBox

 

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

bubuko.com,布布扣

 

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();
            }
        });
        
    }
}

 

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

标签:android   style   blog   http   color   io   java   ar   2014   

原文地址:http://www.cnblogs.com/weijj/p/3946221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!