如何保持在单选按钮的选择值? [英] How to hold the selected value in the radio button?

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

问题描述

我在做测验的应用程序。我已经设置50个问题,每个问题包含4个选项(单选按钮组)与透水和下一步按钮。选择答案时,我需要的是,去了下一个问题,他再次来到previous问题,那个时候他选择了perviously答案应该是处于选择状态。
不过,用户选择的值,我的getText()
如何实现这一点?任何一个可以帮助我..

在此先感谢..

  ImageView的previousbtn1 =(ImageView的)findViewById(R.id.prv_btn);
        previousbtn1.setOnClickListener(新Button.OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                如果(J&。1)
                {
                    Toast.makeText(Question.this,第一问题,Toast.LENGTH_SHORT).show();
                }
                其他
                {
                    --j;
                    --num;
                    --m;
                    TextView的txtque =(的TextView)findViewById(R.id.que_txt);
                    txtque.setText(Q+ NUM +)+ ques1.get(J));
                    --k;
                    btn_practice1.setText(answ1.get((K * 4)+ 0));
                    btn_practice2.setText(answ1.get((K * 4)+ 1));
                    btn_practice3.setText(answ1.get((K * 4)+ 2));
                    btn_practice4.setText(answ1.get((K * 4)+ 3));
                }
            }
        });


解决方案

基本上它看起来像你的问题不是如何建立一个单选按钮活跃,但整体的概念 - 让我描述我会做到这一点:

最简单的办法

使用任何类型的列表或地图(preferably的HashMap,其中关键是问题数量和价值是答案值(1,2或3 - 简单的整数))的。然后,当你在例如第2个问题,你检查是否有alrerady为重点2项时,如果你让你读出的价值和actviate相应的单选按钮(这是通过<$ C $做的方式C> radio1.setChecked(真)) - 如果不是只要用户点击一个单选按钮雁存储在哈希表

的也许更好的方法:)

几乎和以前一样,但不是简单的整型为你使用对象对你的答案(也许也是问题,那么你可以直接在问题存储答案)答案 - 这就是如果答案/问题的好是去是比较复杂的,然后简单的1,2或3。

边注

如果AFTER应用程序被关闭了用户的回答应该是可用的,你必须将它们存储在任何一个SQLite数据库或共享preferences。我认为,SQLite是为了这个美好的,因为这些ansers并没有真正的共享preferences /设置,但你一定需要一些更多的努力。


既然你问它:

 开关(回答){
    情况1:
        radio1.setChecked(真)
    打破;
    案例2:
        radio2.setChecked(真)
    打破;
    案例3:
        radio3.setChecked(真)
    打破;
    默认:
    打破;
}

I'M doing quiz app. I have set 50 questions and each question contains 4 options(radio group) with pervious and next button. What i need is when select the answer and goes for next question and again he comes to previous question that time what he select perviously that answer should be in selected state. But user selected value i getText() How to implement this ? Any one help me..

Thanks in advance..

ImageView previousbtn1 = (ImageView) findViewById(R.id.prv_btn);
        previousbtn1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                if ( j < 1 )
                {
                    Toast.makeText(Question.this, "First Question", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    --j;
                    --num;
                    --m;
                    TextView txtque = (TextView) findViewById(R.id.que_txt);
                    txtque.setText("Q" + num + ")" + ques1.get(j));
                    --k;
                    btn_practice1.setText(answ1.get((k * 4) + 0));
                    btn_practice2.setText(answ1.get((k * 4) + 1));
                    btn_practice3.setText(answ1.get((k * 4) + 2));
                    btn_practice4.setText(answ1.get((k * 4) + 3));
                }
            }
        });

解决方案

Basically it looks like your problem is not how to set a radio button active, but the whole concept - so I describe how I'd do it:

The easy way

Use any kind of list or map (preferably hashmap, where the key is the question number and the value is the answer value (1,2 or 3 - simple integers)). Then when you're at question 2 for example, you check if there is alrerady an entry for the key 2, if so you you read out the value and actviate the corresponding radio button (this is by the way done with radio1.setChecked(true)) - if not as soon as the user clicks a radio button the anser is stored in the hash map.

The maybe better way :)

Almost same as before, but instead of simple "ints" as answers you use objects for your answers (maybe also for the questions, then you could store your answers directly in the question) - this is good if the answers / questions are gonna be more complicated then simple "1,2 or 3".

Side note

If the answers of the users should be available AFTER the app is closed, you'd have to store them in either a sqlite db or in shared preferences. I think sqlite is better for this, since these ansers are not really SharedPreferences/Settings, but you surely need a little more effort.


Since you ask for it:

switch(answer){
    case 1:
        radio1.setChecked(true)
    break;
    case 2:
        radio2.setChecked(true)
    break;
    case 3:
        radio3.setChecked(true)
    break;
    default:
    break;
}

这篇关于如何保持在单选按钮的选择值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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