Java 在Android按钮和文本视图上显示阵列

Java 在Android按钮和文本视图上显示阵列,java,android,arrays,button,syntax,Java,Android,Arrays,Button,Syntax,我目前正在做Android多功能问答游戏,但我对如何在文本视图上显示所有六个问题和在按钮上显示四个答案有点困惑?另外,我不知道如何匹配Android中的正确/错误答案(因为它涉及Android语法) PS:当玩家按下按钮时,其正确或不正确的玩家是否会移动到下一个新问题+答案,如果我想将问题和答案存储在txt文件中,是否可能 以下是我到目前为止所做的 公共类play扩展了活动实现OnClickListener{ private int correctanswers; private TextVie

我目前正在做Android多功能问答游戏,但我对如何在文本视图上显示所有六个问题和在按钮上显示四个答案有点困惑?另外,我不知道如何匹配Android中的正确/错误答案(因为它涉及Android语法)

PS:当玩家按下按钮时,其正确或不正确的玩家是否会移动到下一个新问题+答案,如果我想将问题和答案存储在txt文件中,是否可能

以下是我到目前为止所做的

公共类play扩展了活动实现OnClickListener{

private int correctanswers;
private TextView questionstextview;
private TextView questionnumber;
private TextView playerfeedback;
private int totalanswer;
private int score;
private List<Question> QuestionList;

Button answer1,answer2,answer3,answer4;
Button AnswerButtons [] = {answer1,answer2,answer3,answer4};


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);

        QuestionList = new ArrayList<Question>();
        ArrayList <String> answer = new ArrayList<String>();

        answer.add("8");
        answer.add("9");
        answer.add("3");
        answer.add("1");
        QuestionList.add(new Question("what is 4+4", answer, 0));
        answer.add("17");
        answer.add("20");
        answer.add("15");
        answer.add("14");
        QuestionList.add(new Question("what is 7+8?", answer, 3));
        answer.add("20");
        answer.add("30");
        answer.add("19");
        answer.add("34");
        QuestionList.add(new Question("what is 10+10?", answer, 0));
        answer.add("12");
        answer.add("11");
        answer.add("13");
        answer.add("14");
        QuestionList.add(new Question("what is 6+6?", answer, 0));
        answer.add("6");
        answer.add("5");
        answer.add("4");
        answer.add("7");
        QuestionList.add(new Question("what is 4+3?", answer, 3));
        answer.add("7");
        answer.add("9");
        answer.add("10");
        answer.add("11");
        QuestionList.add(new Question("what is 3+7?", answer, 2));


        questionstextview = (TextView) findViewById (R.id.questionstextview);

        questionnumber = (TextView) findViewById (R.id.questionnumber);


        View AnswerButton1 = findViewById(R.id.answerbutton1);
        AnswerButton1.setOnClickListener(this);
        View AnswerButton2 = findViewById(R.id.answerbutton2);
        AnswerButton2.setOnClickListener(this);
        View AnswerButton3 = findViewById(R.id.answerbutton3);
        AnswerButton3.setOnClickListener(this);
        View AnswerButton4 = findViewById(R.id.answerbutton4);
        AnswerButton4.setOnClickListener(this);

    }


private void ButtonPress (Button answerButton){



}

public play() {

}


@Override
public void onClick(View v) {


}
private;
私人文本视图问题文本视图;
私有文本视图问题编号;
私有文本视图播放器反馈;
私人回答;
个人智力得分;
私人清单问题清单;
按钮应答1、应答2、应答3、应答4;
按钮应答按钮[]={answer1,answer2,answer3,answer4};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
QuestionList=新的ArrayList();
ArrayList answer=新的ArrayList();
答复。添加(“8”);
答复。添加(“9”);
答复。添加(“3”);
答复。添加(“1”);
问题列表。添加(新问题(“什么是4+4”,答案,0));
答复。添加(“17”);
答复.添加(“20”);
答复.添加(“15”);
答复。添加(“14”);
问题列表。添加(新问题(“7+8是什么?”,答案,3));
答复.添加(“20”);
答复.添加(“30”);
答复。添加(“19”);
答复.添加(“34”);
问题列表。添加(新问题(“10+10是什么?”,答案,0));
答复。添加(“12”);
答复.添加(“11”);
答复。添加(“13”);
答复。添加(“14”);
问题列表。添加(新问题(“6+6是什么?”,答案,0));
答复。添加(“6”);
答复。添加(“5”);
答复。添加(“4”);
答复。添加(“7”);
问题列表。添加(新问题(“什么是4+3?”,答案,3));
答复。添加(“7”);
答复。添加(“9”);
答复。添加(“10”);
答复.添加(“11”);
问题列表。添加(新问题(“3+7是什么?”,答案,2));
questionstextview=(TextView)findViewById(R.id.questionstextview);
questionnumber=(TextView)findViewById(R.id.questionnumber);
视图AnswerButton1=findViewById(R.id.AnswerButton1);
AnswerButton1.setOnClickListener(此);
视图AnswerButton2=findViewById(R.id.AnswerButton2);
AnswerButton2.setOnClickListener(此);
视图AnswerButton3=findViewById(R.id.AnswerButton3);
AnswerButton3.setOnClickListener(此);
视图AnswerButton4=findViewById(R.id.AnswerButton4);
AnswerButton4.setOnClickListener(此);
}
专用无效按钮按下(按钮应答按钮){
}
公演{
}
@凌驾
公共void onClick(视图v){
}

使用单选按钮选择正确答案,然后使用按钮继续下一个问题

优势

  • 从一个活动到另一个活动的移动将更加真实(就像翻开一页)
  • 给人一种向上移动一层的感觉!!;)
  • 最重要的是更容易实现(我发现这样做更容易)
  • 活动布局的xml代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.stackoverflow.MainActivity" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <RadioGroup
    android:id="@+id/rad_btn_group1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onRadioButtonClicked" >
    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/radio_button1" />
    
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:text="@string/radio_button2" />
    
    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:text="@string/radio_button3" />
    
    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_button4"
        android:layout_marginTop="21dp" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:text="@string/btn_submit" />
    
    </RadioGroup>
    
    </RelativeLayout>
    
    public class MainActivity extends Activity implements OnClickListener{
    
    
    Button btn ;
    RadioGroup selectionGroup;
    public static int score = 0;//to access it in other class as well to ++ or --
    boolean answerAttempt = false; //to penalize player changing the answer      ;)
    
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
        selectionGroup = (RadioGroup) findViewById(R.id.rad_btn_group1);
        selectionGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //because we know radiobutton3 is contains the right answer :)
                if(answerAttempt)
                {
                    score -= 2;
                }
                else if (R.id.radioButton3 == checkedId && !answerAttempt)
                {
                    ++score;
                    answerAttempt = true; //meaning first attempt is made by the player
                }
                else
                {
                    --score;
                    answerAttempt = true; //meaning first attempt is made by the player
                }
    
            }
        });
    }
    
    
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    
    
    switch(v.getId())//this is the id of the button clicked (in our case submit button)
        {
        case R.id.button1://this is the id of the submit button in xml
            //start another activity from here for next question on another activity :)
    
            break;
        }
    
    }
    }
    
    编辑

    自动转到下一个活动的代码

    e@Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            //because we know radiobutton3 is contains the right answer :)
            if (R.id.radioButton3 == checkedId)
            {
                ++score;
                //goes to the next question :)
              startActivity(new Intent(getBaseContext(), next_question_activity.class));
    
            }
            else
            {
                //goes to the next question without troubling the score :)
              startActivity(new Intent(getBaseContext(), next_question_activity.class));
    
            }
    
        }
    });
    

    为什么不将ListView与文本视图一起使用?我只想使用一个文本视图来显示所有六个问题。因此,每次用户按下回答按钮(4个回答按钮)时,文本视图将显示下一个问题。非常感谢您的回答,我将尝试使用单选按钮。但我不允许使用按钮继续下一个回答(作业规范的一部分),在用户选择答案后,它必须自动进入下一个问题。对不起,我是这里的新手;)@user3829911然后不要使用按钮,只需在单选按钮中实现“转到其他问题”,单击侦听器:)一旦按下单选按钮,您将进入下一个问题:)(永远不要忘记投票或接受;)@user3829911为您编辑:)您也可以接受其他问题的答案!!;)顺便说一句,谢谢!!不客气,不管怎样,可以将问题和答案存储在txt文件(或任何其他类型的文件)中吗?如何在同一文本视图中显示不同的问题,而不必移动到不同的类?