• android学习--radiogroup学习


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

    效果图:

    image

    main_layout.xml

    <?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>

    MainActivity.java

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

    hi,完成运行

    image

  • 相关阅读:
    [CareerCup] 8.1 Implement Blackjack 实现21点纸牌
    [LeetCode] H-Index 求H指数
    [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
    [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线
    Ionic实战一:Ionic仿照微信项目
    ionic3+angular4+cordova 项目实例
    Android 给EditText添加下划线
    浅谈移动优先的跨终端Web 解决方案
    Android环信即时通讯集成坑爹 注册报错208解决
    Android自定义控件 -- 带边框的TextView
  • 原文地址:https://www.cnblogs.com/cmzcheng/p/3656718.html
Copyright © 2020-2023  润新知