首页 > 移动平台 > 详细

android学习--radiogroup学习

时间:2014-04-10 22:36:47      阅读:510      评论:0      收藏:0      [点我收藏+]

这个阶段在学习android的相关基本UI现将相关练习的代码粘贴在此便于后期学习之用(radio控件)

效果图:

 

bubuko.com,布布扣

main_layout.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sex" />

    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/boy" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/girl" />
    </RadioGroup>

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnSel" />

</LinearLayout>
bubuko.com,布布扣

MainActivity.java

bubuko.com,布布扣
package com.skycc.radio;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
 * 

 * @Description: 单选按钮练习

 * @author:skycc

 * @time:2014-4-10 下午3:53:15
 */
public class MainActivity extends Activity {
    private Button button;
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        button = (Button) this.findViewById(R.id.btn);
        group = (RadioGroup) this.findViewById(R.id.sex);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int len = group.getChildCount();//获取这个group中的radio数
                String msgString = "";
                for (int i = 0; i < len; i++) {
                    RadioButton radioButton = (RadioButton) group.getChildAt(i);//将每个group中的radio进行遍历,并判断是否被选中
                    if (radioButton.isChecked()) {

                        msgString = "选中了性别" + radioButton.getText().toString();
                        break;
                    }
                }
                Toast.makeText(MainActivity.this, msgString, 1).show();
            }
        });
    }
}
bubuko.com,布布扣

hi,完成运行

bubuko.com,布布扣

android学习--radiogroup学习,布布扣,bubuko.com

android学习--radiogroup学习

原文:http://www.cnblogs.com/cmzcheng/p/3656718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!