RadioGroup与RadioButton的搭配使用

来源:互联网 发布:读小说软件 编辑:程序博客网 时间:2024/04/30 05:02
    关键词:id,自定义,提示信息
    RadioButton是单选按钮,必须与RadioGroup搭配使用,
     <RadioGroup
        android:id="@+id/radiogroup">//必须定义各个的id
        <RadioButton
            android:id="@+id/manbutton"
            android:text="男"
            />
        <RadioButton
            android:id="@+id/wemenbutton"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>
    自定义:1.可以是就着系统已定义的小圆圈
            2.设置背景background,然后根据背景的来源设定不同的选项
            android:background="@drawable/login_button"
            3.使用drawable属性,添加其他的资源
             android:drawableLeft="@drawable/login_button"
            4.在RadioButton里添加button的属性
             android:button="@drawable/pic"
    提示信息:在使用时我们想要每点击一次,系统提醒我们的选项,这时需要设定提示信息
        首先,得到按钮的id
         mRadioButtonman=(RadioButton) findViewById(R.id.manbutton);
        然后,设定监听
          mRadioGroup.setOnCheckedChangeListener
        监听按钮
        RadioGroup.OnCheckedChangeListener()
        当按钮点击做出改变时
        onCheckedChanged
        
          mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            public void onCheckedChanged(RadioGroup group,int checkedId){
                if(checkedId==R.id.manbutton){
                    RadioButton radioButton=(RadioButton)group.getChildAt(0);//得到GroupButton的第一个子元素 男
                    System.out.println("你选中的是"+radioButton.getText());
                    Toast.makeText(Radio_Group_Activity.this,"你选中的是"+radioButton.getText(),Toast.LENGTH_SHORT).show();//Toast方法
                }
                if(checkedId==R.id.wemenbutton){
                    RadioButton radioButton=(RadioButton)group.getChildAt(1);
                    System.out.println("你的选择是"+radioButton.getText());
                       Snackbar.make(group,你的选择是"+radioButton.getText(),Snackbar.LENGTH_SHORT).show();//Snackbar方法

                }
            }
        });
       
0 0
原创粉丝点击