自定义单选按钮 [英] Custom Radio button

查看:121
本文介绍了自定义单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的自定义单选按钮。它工作正常(因为它是在options.xml定义),但是当我从options.xml切换到main.xml中,原来默认的,意味着它没有更多的强调。它应该工作一样,直到我preSS它不应该把默认..这里是radiobutton_selector.xml

i'm working on custom radio button. it works fine(as it is defined in options.xml) but when i switch from options.xml to main.xml, it turns default, means it is no more highlighted. it should work like until i press it it should not turn to default.. here is radiobutton_selector.xml

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/radio_down" android:state_checked="true"/>
        <item android:drawable="@drawable/radio" android:state_checked="false"/>
        </selector>

我在options.xml使用这些调用单选按钮设置。

i'm using these in options.xml to call radio button settings.

        <RadioGroup
            android:id="@+id/sound"
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:layout_gravity="right"
            android:layout_marginRight="0dp"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:padding="0dp" >

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:button="@drawable/radiobutton_selector"
                android:id="@+id/on"
                />

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:button="@drawable/radiobutton_selector"
                android:id="@+id/off"
                />
        </RadioGroup>

请帮我找出这个问题。在此先感谢!!!!!

Please help me to figure out the issue. Thanks in advance !!!!!

推荐答案

您可以像这样为你的 RadioGroup中。 你需要保存你选择哪个单选按钮,为您可以使用一个变量如下图所示。

You can do like this for your RadioGroup. you need to save which radio Button you have selected ,for that you can use one variable like below.

int check_radio=-1;
public static final int RADIO_BUTTON_ON=1;
public static final int RADIO_BUTTON_OFF=2;

       mRadioGroup
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {

                        switch(checkedId){

                                            case R.id.on:
                                            //Radio Button on is True
                                            check_radio=RADIO_BUTTON_ON;
                     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                     SharedPreferences.Editor editor = settings.edit();
                      editor.putInt("RadioButton", RADIO_BUTTON_ON);
                     // Commit the edits!
                     editor.commit();
                                            break;
                                           case R.id.off:
                                          //Radio Button off is True
                                           check_radio=RADIO_BUTTON_OFF;
                       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                     SharedPreferences.Editor editor = settings.edit();
                      editor.putInt("RadioButton", RADIO_BUTTON_OFF);
                     // Commit the edits!
                     editor.commit();
                                          break;
                    }
                });

现在你乳清活动的简历,您可以检查一个条件如下图所示。 从共享prefrence获得值中包含code;

Now whey your Activity 's Resume you can check one condition like below. Get Value from SharedPrefrence Like below code;

   //If you have save your value in SharedPrefrence it will return your stored int value.
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   check_radio = settings.getInt("RadioButton", -1);


  if(check_radio==RADIO_BUTTON_ON){
     mRadioOn.setChecked(true);
  }else if(check_radio==RADIO_BUTTON_OFF){
    mRadioOff.setChecked(true);
  }

您可以通过下面的步骤使用共享preferences

you can use SharedPreferences by below step

   public static final String PREFS_NAME = "MyPrefsFile";
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

这篇关于自定义单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆