【问题标题】:Display array on Android button and textview在 Android 按钮和 textview 上显示数组
【发布时间】:2014-09-18 23:40:14
【问题描述】:

我目前正在做 Android 多项测验游戏,但我对如何在 TextView 上显示所有六个问题和在按钮上显示四个答案有点困惑?另外,我不确定如何在 Android 中匹配正确/错误的答案(因为它涉及 Android 语法)。

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

这是我到目前为止所做的......

public class play extends Activity 实现 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) {


}

【问题讨论】:

  • 为什么不使用 ListView 和 TextViews?
  • 我只想使用一个文本视图来显示所有六个问题。所以每次用户按下答案按钮(4 个答案按钮)时,TextView 都会显示下一个问题。

标签: java android arrays button syntax


【解决方案1】:

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

优势

  1. 从一个活动到另一个活动将更加真实(就像翻页一样)
  2. 给人一种升一级的感觉!! ;)
  3. 最重要的是更容易实现(我觉得那样更容易)

活动布局的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>

游戏逻辑的java代码如下:

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));

        }

    }
});

【讨论】:

  • 非常感谢您的回答,我会尝试使用单选按钮.. 但在用户之后,我不允许使用按钮继续下一个答案(作业规范的一部分)选择一个答案,然后它必须自动进入下一个问题。对不起,我是新手;)
  • @user3829911 然后不要使用该按钮,只需在单选按钮单击侦听器中执行“转到其他问题”即可:) 按下单选按钮后,您将继续进行下一个问题 :) (永远不要忘记投票或接受;))
  • @user3829911 为您进行了编辑:) 您也可以接受其他问题的答案! ;) 顺便谢谢!!
  • 不客气,是否可以将问题和答案存储在 txt 文件(或任何其他类型的文件)中?
  • 如何在同一个 TextView 中显示不同的问题,而不必移动到不同的类?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
相关资源
最近更新 更多