RadioButton的实现

作者:余俊


      老师在之前的课上讲了怎么样在布局文件中添加RadioButton控件,但是具体的实现过程没有提及。这篇文章主要给大家分享怎么样具体实现。文章基于五子棋的配置信息部分改写的,下面给大家分享设置走棋的先后顺序和难度系数部分。

首先来看XML文件:

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

    android:orientation="vertical"

    >

<!--

RadioButton - 单选框控件

RadioGroup - 对其内的单选框控件做分组

checkedButton - 指定组内被选中的单选框的 ID

-->

<RadioGroup

android:id="@+id/radioGroup"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checkedButton="@+id/rad1"

android:gravity="center_vertical|center_horizontal"

android:orientation="horizontal">

<RadioButton

android:id="@+id/rad1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="初级" >

</RadioButton>

<RadioButton

android:id="@+id/rad2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="中级" >

</RadioButton>

<RadioButton

android:id="@+id/rad3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="高级" >

</RadioButton>

</RadioGroup>

 

<RadioGroup

android:id="@+id/AfterGroup"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checkedButton="@+id/afterrad1"

android:gravity="center_vertical|center_horizontal"

android:orientation="horizontal">

<RadioButton

android:id="@+id/afterrad1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="黑棋先手" >

</RadioButton>

<RadioButton

android:id="@+id/afterrad2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="白棋先手" >

</RadioButton>

</RadioGroup>

</LinearLayout>



接下来看看.java文件中的主要部分:


private SharedPreferences sp;

private RadioGroup radioGroup ,AfterGroup;

private RadioButton rad1,rad2,rad3,afterrad1,afterrad2;

       

      sp=getSharedPreferences("OPTION",MODE_PRIVATE);

 

radioGroup=(RadioGroup)findViewById(R.id.radioGroup);

            AfterGroup=(RadioGroup)findViewById(R.id.AfterGroup);

           

            rad1=(RadioButton)findViewById(R.id.rad1);

            rad2=(RadioButton)findViewById(R.id.rad2);

            rad3=(RadioButton)findViewById(R.id.rad3);

           

            afterrad1=(RadioButton)findViewById(R.id.afterrad1);

            afterrad2=(RadioButton)findViewById(R.id.afterrad2);

 

//难度配置信息和走棋先后顺序

        String NanduConf=sp.getString("OPTION_NANDU", "初级");

        setNanduConf(NanduConf);

        String ShunXu=sp.getString("OPTION_SHUNXU", "黑棋先走");

        setShunXu(ShunXu);

       //监听难度选项信息

       radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                  

                   public voidonCheckedChanged(RadioGroup group, int checkedId) {

                            // TODOAuto-generated method stub

                            SharedPreferences.Editoreditor=sp.edit();

                            if(checkedId==rad1.getId())

                            {

                                     editor.putString("OPTION_NANDU", "初级");

                                     setNanduConf("初级");

                            }

                            if(checkedId==rad2.getId())

                            {

                                     editor.putString("OPTION_NANDU", "中级");

                                     setNanduConf("中级");

                            }

                            else

                            {

                                     editor.putString("OPTION_NANDU", "高级");

                                     setNanduConf("高级");

                            }

                                     //确认修改

                            editor.commit();

                   }

         });

       //设置玩家走棋的先后顺序

       AfterGroup.setOnCheckedChangeListener(newOnCheckedChangeListener() {

                  

                   public voidonCheckedChanged(RadioGroup group, int checkedId) {

                            // TODOAuto-generated method stub

                       SharedPreferences.Editor editor=sp.edit();

                       if(checkedId==afterrad1.getId())

                       {

                          editor.putString("OPTION_SHUNXU","黑棋先走");

                           setShunXu("黑棋先走");

                       }

                       else

                       {

                               editor.putString("OPTION_SHUNXU", "白棋先走");

                               setShunXu("白棋先走");

                       }

                       editor.commit();

                   }

         });}

 

  //设置难度按钮

        public void setNanduConf(String nandu)

        {

               if("初级".equals(nandu))

               {

                        rad1.setChecked(true);

                        rad2.setChecked(false);

                        rad3.setChecked(false);

                }

               if("中级".equals(nandu))

               {

                        rad1.setChecked(false);

                        rad2.setChecked(true);

                        rad3.setChecked(false);

               }

               else

               {

                        rad1.setChecked(false);

                        rad2.setChecked(false);

                        rad3.setChecked(true);

               }

        }

        //设置走棋的顺序

        public void setShunXu(String shunxu)

        {

               if("黑棋先走".equals(shunxu))

               {

                        afterrad1.setChecked(true);

                        afterrad2.setChecked(false);

               }

               else

               {

                        afterrad1.setChecked(false);

                        afterrad2.setChecked(true);

               }

        }

}

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值