• RadioButton练习(android)


    Radio 单选是由 RadioGroup 与 RadioButton 组成的,这个很简单,看代码就会了

    main.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
    ="fill_parent"
        android:orientation
    ="vertical" >

        <TextView
            
    android:layout_width="fill_parent"
            android:layout_height
    ="wrap_content"
            android:text
    ="你的性别:请选择答案 " />
        <RadioGroup 
            
    android:id="@+id/rg1"
            android:layout_width
    ="wrap_content"
            android:layout_height
    ="wrap_content"
            android:orientation
    ="vertical"
            android:layout_x 
    = "3px"
            android:layout_y 
    = "54px"
            
    >
            <RadioButton
                
    android:id="@+id/rb1"
                android:layout_width
    ="wrap_content"
                android:layout_height
    ="wrap_content"
                android:text
    ="男生"
                 
    />
             <RadioButton
                
    android:id="@+id/rb2"
                android:layout_width
    ="wrap_content"
                android:layout_height
    ="wrap_content"
                android:text
    ="女生"
                 
    />
        </RadioGroup>
        <Button 
            
    android:id="@+id/btn"
            android:layout_width
    ="wrap_content"
            android:layout_height
    ="wrap_content"
            android:text
    ="测试"
            
    />
    </LinearLayout>

    代码

    package zziss.android.radiotest;

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

    public class RadioTestActivity extends Activity {
        /** Called when the activity is first created. */
        RadioGroup rg1 ;
        RadioButton rb1;
        RadioButton rb2;
        Button      ibtn;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            rg1 = (RadioGroup)this.findViewById(R.id.rg1);
            rb1 = (RadioButton)this.findViewById(R.id.rb1);
            rb2 = (RadioButton)this.findViewById(R.id.rb2);
            
            rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    if (checkedId == rb1.getId())
                    {
                        ShowInfo(rb1.getText().toString());
                    }
                    if (checkedId == rb2.getId())
                    {
                        ShowInfo(rb2.getText().toString());
                    }
                }
            });
            
            ibtn = (Button)this.findViewById(R.id.btn);
            ibtn.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = rg1.getCheckedRadioButtonId();
                    if (id != -1)
                        showIdText(id);
                    else
                        ShowInfo("请选择");
                }
            });
        }
        
        private void ShowInfo(String str)
        {
            Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
            toast.show();
        }
        private void showIdText(int aId)
        {
            ShowInfo(((RadioButton)this.findViewById(aId)).getText().toString());
        }
        
    }
  • 相关阅读:
    POJ-1502-MPI Maelstrom
    POJ-3259-Wormholes
    【BZOJ4399】—膜法少女LJJ(线段树合并)
    省选模板复习—【计算几何】
    【BZOJ2115】【WC2011】—Xor(线性基)
    【洛谷P5290】【十二省联考2019】春节十二响(贪心+启发式合并)
    【BZOJ5461】 【PKUWC2018】—Minimax(线段树合并优化dp)
    【LOJ#3043】【洛谷P5280】【ZJOI2019】—线段树(计数dp+线段树)
    【省选模拟】—猎人杀(概率dp)
    【BZOJ4828】【HNOI2017】—大佬(LmyAKIOI!)
  • 原文地址:https://www.cnblogs.com/zziss/p/2288220.html
Copyright © 2020-2023  润新知