• 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());
        }
        
    }
  • 相关阅读:
    51nod 1174 区间最大值(RMQ and 线段树)
    Round #447(Div 2)
    51nod 2006 飞行员匹配
    75.Java异常处理机制throws
    74.Java异常处理机制
    emmm
    数据库关系代数
    汇编实验二 2进制转16进制
    汇编实验一 显示字符串
    JustOj 1386: 众数的数量
  • 原文地址:https://www.cnblogs.com/zziss/p/2288220.html
Copyright © 2020-2023  润新知