如何在android中保存单选按钮的状态

How to save state of radio button in android(如何在android中保存单选按钮的状态)
本文介绍了如何在android中保存单选按钮的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存在 android 中单击的单选按钮的状态.当用户进入下一个问题时,它必须保存状态,所以当他回到上一个状态时,它必须显示所选选项.

I want to save the state of radio button clicked in android. When user goes to next question it must save state so when he comes back to previous state it must show the selected option.

推荐答案

使用共享首选项,保存单选按钮的状态,并在其他活动中检索数据.

Use Shared Preferences, To save your radio button's state,and you get to retrieve data in other activities.

这是java类:

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class ABCD extends Activity{


    private int ival;
    public static int glob=1;
    static SharedPreferences sPref;
    private SharedPreferences.Editor sE;
    RadioGroup rbMain;
    RadioButton rb1,rb2,rb3,rb4,rb5;   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        rbMain = (RadioGroup) findViewById(R.id.rgMain);
        rb1 = (RadioButton)findViewById(R.id.radio1);
        rb2 = (RadioButton)findViewById(R.id.radio2);
        rb3 = (RadioButton)findViewById(R.id.radio3);
        rb4 = (RadioButton)findViewById(R.id.radio4);
        rb5 = (RadioButton)findViewById(R.id.radio5);

        sPref = getSharedPreferences("Pref",0);
        sE = sPref.edit();
        ival = sPref.getInt("Pref", 0);

        if(ival == R.id.radio1){
            rb1.setChecked(true);
            glob=1;
        }else if(ival == R.id.radio2){
            rb2.setChecked(true);   
            glob=2;
        }else if(ival == R.id.radio3){
            rb3.setChecked(true);
            glob=3;
        }else if(ival == R.id.radio4){
            rb4.setChecked(true);   
            glob=4;
        }else if(ival == R.id.radio5){
            rb5.setChecked(true);
            glob=5;
        }

        rbMain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            int state = 0;
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.radio1){
                    sE.clear();
                    sE.putInt("Pref", checkedId);
                    sE.apply();
                }else if(checkedId == R.id.radio2){
                    sE.clear();
                    sE.putInt("Pref", checkedId);
                    sE.apply();
                }else if(checkedId == R.id.radio3){
                    sE.clear();
                    sE.putInt("Pref", checkedId);
                    sE.apply();
                }else if(checkedId == R.id.radio4){
                    sE.clear();
                    sE.putInt("Pref", checkedId);
                    sE.apply();
                }else if(checkedId == R.id.radio5){
                    sE.clear();
                    sE.putInt("Pref", checkedId);
                    sE.apply();
                }

            }
        });

    }   

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        sPref = getSharedPreferences("Pref",0);
        sE = sPref.edit();
        ival = sPref.getInt("Pref", 0);

        if(ival == R.id.radio1){
            rb1.setChecked(true);
            glob=1;
        }else if(ival == R.id.radio2){
            rb2.setChecked(true);   
            glob=2;
        }else if(ival == R.id.radio3){
            rb3.setChecked(true);
            glob=3;
        }else if(ival == R.id.radio4){
            rb4.setChecked(true);   
            glob=4;
        }else if(ival == R.id.radio5){
            rb5.setChecked(true);
            glob=5;
        }
    }

}

这是我的 XML 布局:

and this is my XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  android:background="@color/white"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >
 <RadioGroup android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal" android:layout_height="wrap_content"
        android:layout_weight="1" android:id="@+id/rgMain">
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio1" android:layout_height="wrap_content"
            android:checked="true" android:text="1" android:textColor="@color/back">         
        </RadioButton>
        <RadioButton android:id="@+id/radio2"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="2" android:textColor="@color/back">            
        </RadioButton>
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio3" android:layout_height="wrap_content"
            android:text="3" android:textColor="@color/back">            
        </RadioButton>
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio4" android:layout_height="wrap_content"
            android:text="4" android:textColor="@color/back">            
        </RadioButton>
        <RadioButton android:layout_width="wrap_content"
            android:id="@+id/radio5" android:layout_height="wrap_content"
            android:text="5" android:textColor="@color/back">            
        </RadioButton>
    </RadioGroup>
</LinearLayout>

</ScrollView>

而且,这可以帮助您做任何您需要的事情.

And, this can help you to do whatever you need.

这篇关于如何在android中保存单选按钮的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)