【问题标题】:How to write onclick action for statically created radio buttons in android?如何为android中静态创建的单选按钮编写onclick动作?
【发布时间】:2013-04-16 09:31:10
【问题描述】:

我正在开发 android 测验应用程序。在我的屏幕上,我正在使用下一个按钮显示一个问题和选项(静态 4 个单选按钮)。即当再次单击下一个按钮时 将显示下一个问题及其选项。其实我有一个字符串

 str = |ques1@opid1@option1@opid2@option2@opid3@option3@opid4@opotion4|ques2@opid1@option1@opid2@option2@opid3@option3@opid4@opotion4|ques3@opid1@option1@opid2@option2@opid3@option3@opid4@opotion4 

现在我用“|”分割了 str 数组并将数据存储到str1中。 所以,

str1[1] = 问题1@opid1@option1@opid2@option2@opid3@option3@opid4 str1[2] = ques2@opid1@option1@opid2@option2@opid3@option3@opid4 .........

Now again I splitted the str1 array with "@" and stored the data in str2.
So,

 str2[0] = ques1
          str2[1] = opid1
    str2[2] = option1
    str2[3] = opid2
    str2[4] = option2
    str2[5] = opid3
    str2[6] = option3
    str2[7] = opid4
    str2[8] = option4

So after getting these values i am setting it to 
tv.setText(str2[0]);
answer1.setText(str2[2]);
answer2.setText(str2[4]);
answer3.setText(str2[6]);
answer4.setText(str2[8]);

然后在下一个按钮单击动作中,我编写了以下代码。

int i = 1;
public void next(View v) {

    if (i < str1.length - 1) {
        i++;
        str3 = str1[i].trim().split("[@]");             
        tv.setText(str3[0]);            
        answers.check(-1);
        answer1.setText(str3[2]);
        answer2.setText(str3[4]);
        answer3.setText(str3[6]);
        answer4.setText(str3[8]);       
    }

现在到这里一切都很好。我的下一个要求是获取选定的选项值并将其存储在一个数组中。我还需要将选项 id 值设置为单选按钮 选择并再次获取该 id 值。即假设对于第一个问题,我选择了第二个单选按钮,所以我想将该选项 id 设置为 radiobtn.setid(opid2) 并希望获得该选定的单选按钮 id。由于我静态地保留了单选按钮,因此我不知道如何设置和获取单选按钮的 id 以及如何将 onclick 操作写入单选按钮。 我正在为此苦苦挣扎。请任何帮助将非常感激。

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以试试下面的

    if(rd.isChecked()==true) // rd is the radio button
     {
      String selected =rd.getText().toString();// get the radio button text
    
     }
    

    要设置 id,您可以使用 rd.setId(intvalue) 并获取 id rd.getId()。

    【讨论】:

      【解决方案2】:

      请这样使用。

          if(arg0==btnnext)
          {
          Log.v(TAG+"onClick", "onClick method call");
          //btnback.setVisibility(View.VISIBLE);
      
          if(rboption1.isChecked())
          {
              Log.v(TAG+".onClick", "option 1 selected");
      
              check=1;
          }
          else if(rboption2.isChecked())
          {
              Log.v(TAG+".onClick", "option 2 selected");
              check=2;
          }
          else if(rboption3.isChecked())
          {
              Log.v(TAG+".onClick", "option 3 selected");
              check=3;
          }
      
          else if(rboption4.isChecked())
          {
              Log.v(TAG+".onClick", "option 4 selected");
              check=4;
          }
      
          else
          {
              Log.v(TAG+".onClick", "No any option is selected");
              check=0;
          }
      
          if(Integer.parseInt(answer1)==check)
          {
              correctans++;
              Log.v(TAG+"onClick", "Correct answer are:" + correctans);
          }
          else if(check == 0)
          {
              unattempted++;
              Log.v(TAG+"onClick", "unattempted questions:" + unattempted);
      
          }
          else 
          {
              incorrectans++;
              Log.v(TAG+"onClick", "Wrong answer are:" + incorrectans);
          }
      
          atempted= correctans+incorrectans;
          attemptedquestions.setText("Attempted : "+atempted);
          // insert into table where that select the 
      
          index++;
      
          if(i++ < (list1.size()-1))
          {
              questionanswerbean=list1.get(i);
              Question=questionanswerbean.getQuestion_text();
              Option1=questionanswerbean.getAnswer_choice_1();
              Option2=questionanswerbean.getAnswer_choice_2();
              Option3=questionanswerbean.getAnswer_choice_3();
              Option4=questionanswerbean.getAnswer_choice_4();
                  Log.v(TAG+".Oncraete", "Viewing Question ->" + Question);
                  Log.v(TAG+".Oncraete", "Viewing Option1 ->" + Option1);
                  Log.v(TAG+".Oncraete", "Viewing Option2 ->" + Option2);
                  Log.v(TAG+".Oncraete", "Viewing Option3 ->" + Option3);
                  Log.v(TAG+".Oncraete", "Viewing Option4 ->" + Option4);
      
                     if ((Question != null) && !Question.trim().equals("")) {
                          question.setText("Question: " +index +":     " + Question.trim());
                      }
      
                      if ((Option1 != null) && !Option1.trim().equals("")) {
      
                          rboption1.setText(Option1.trim());
                      }
      
                      if ((Option2 != null) && !Option2.trim().equals("")) {
      
                          rboption2.setText(Option2.trim());
                      }
      
                      if ((Option3 != null) && !Option3.trim().equals("")) {
      
                          rboption3.setText(Option3.trim());
                      }
                      if ((Option4 != null) && !Option4.trim().equals("")) {
      
                          rboption4.setText(Option4.trim());
                      }   
      
                      radiogroup.clearCheck();
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-25
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多