如何在eclipse中为单选按钮赋值

时间:2014-11-18 02:19:00

标签: android eclipse

我正在开发一个包含10组问题的应用程序。一个问题属于一个页面,因此我有10个不同的页面。每个答案都使用单选按钮。我想问一下,如何在单选按钮上分配值并将值带到10页并在结果页面中显示结果?

package com.project.logicalthinking;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RadioGroup;


public class question1 extends Activity
 {
  private Button Button2;

public void onCreate(Bundle savedInstanceState)
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.question1);

   addListenerRadioButton() ;   
 //Button2
        Button2 = (Button) findViewById(R.id.button1);
        Button2.setOnClickListener((new OnClickListener()
        {

            public void onClick(View v) 
            {
                Intent intent = new Intent(v.getContext(),question2.class);
                startActivityForResult(intent,0);
            }
        }

            ));
 }      
 private void addListenerRadioButton() {

     RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);        
        radioGroup.setOnTouchListener(new OnTouchListener() 
        {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return false;
            }
        });                
      }
         ;  
    }

2 个答案:

答案 0 :(得分:0)

我不是机智大师(如果我错了,有人会纠正我),但是你能为每个定义选项的活动声明静态变量,然后在结果活动中检查静态变量值?

public static int QUESTION1_NO_ANSWER = 0;
public static int QUESTION1_ANSWER1 = 1;
public static int QUESTION1_ANSWER2 = 2;
public static int QUESTION1_ANSWER3 = 3;
public static int selectedAnswer;

然后在你的结果中......

if(QUESTION1.selectedAnswer = QUESTION1_ANSWER2)
    //YAY got question right!
} else {
    //uh oh, it was wrong
}

答案 1 :(得分:-1)

当用户使用单选按钮选择是/否并按“下一步”时,您需要逐个填充问题。是/否可以是radio button,“下一步”只是Button。在“下一步”按钮的OnClickListener中加载了一个新问题。