Android上的按钮,TextView的显示器阵列 [英] Display array on Android button and textview

查看:256
本文介绍了Android上的按钮,TextView的显示器阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做多的Andr​​oid问答游戏,但我如何显示上的按钮上一个TextView所有六个问题和四个答案就有点糊涂了?此外,林不知道如何搭配Android中正确/不正确的答案(因为它涉及到Android的语法)。

PS:当玩家preSS按钮是其正确不正确的球员将移动到下一个新的问题+答案,这将是可能的,如果我想保存在TXT文件中的问题和答案

这是我迄今所做的......

公共类游戏扩展活动实现OnClickListener {

 私人诠释correctanswers;
私人TextView的questionstextview;
私人TextView的questionnumber;
私人TextView的playerfeedback;
私人诠释totalanswer;
私人诠释得分;
私人列表<问题> QuestionList;按钮ANSWER1,ANSWER2,ANSWER3,answer4;
按钮AnswerButtons [] = {ANSWER1,ANSWER2,ANSWER3,answer4};
 @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.play);        QuestionList =新的ArrayList<问题>();
        ArrayList的<串GT;回答=新的ArrayList<串GT;();        answer.add(8);
        answer.add(9);
        answer.add(3);
        answer.add(1);
        QuestionList.add(新课题(什么是4 + 4的答案,0));
        answer.add(17);
        answer.add(20);
        answer.add(15);
        answer.add(14);
        QuestionList.add(新课题(什么是7 + 8?,答案,3));
        answer.add(20);
        answer.add(30);
        answer.add(19);
        answer.add(34);
        QuestionList.add(新课题(什么是10 + 10?,答案,0));
        answer.add(12);
        answer.add(11);
        answer.add(13);
        answer.add(14);
        QuestionList.add(新课题(什么是6 + 6?的回答,0));
        answer.add(6);
        answer.add(5);
        answer.add(4);
        answer.add(7);
        QuestionList.add(新课题(什么是4 + 3?,答案,3));
        answer.add(7);
        answer.add(9);
        answer.add(10);
        answer.add(11);
        QuestionList.add(新课题(什么是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(本);    }
私人无效按钮preSS(按钮answerButton){}公开播放(){}
@覆盖
公共无效的onClick(视图v){
}


解决方案

使用单选按钮来选择正确的答案,并使用按钮进入下一个问题!

优点


  1. 从一个活动到其他的运动将更加真实(好像翻页)

  2. 给移动一个级别的感觉! ;)

  3. 最重要更容易实现(嗯,我觉得它更容易这样)

XML code的活动布局:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=com.stackoverflow.MainActivity><的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/参考hello world/>
< RadioGroup中
机器人:ID =@ + ID / rad_btn_group1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =onRadioButtonClicked>
<单选
    机器人:ID =@ + ID / radioButton4
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =16DP
    机器人:文字=@字符串/ radio_button1/><单选
    机器人:ID =@ + ID / radioButton1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =21dp
    机器人:文字=@字符串/ radio_button2/><单选
    机器人:ID =@ + ID / radioButton2
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =21dp
    机器人:文字=@字符串/ radio_button3/><单选
    机器人:ID =@ + ID / radioButton3
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/ radio_button4
    机器人:layout_marginTop =21dp/><按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop =21dp
    机器人:文字=@字符串/ btn_submit/>< / RadioGroup中>< / RelativeLayout的>

的Java code为游戏逻辑如下:

 公共类MainActivity扩展活动实现OnClickListener {
按钮BTN;
RadioGroup中selectionGroup;
公共静态INT得分= 0; //访问它在其他类以及以++或 -
布尔answerAttempt = FALSE; //惩罚玩家改变的答案;)@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    BTN =(按钮)findViewById(R.id.button1);
    btn.setOnClickListener(本);
    selectionGroup =(RadioGroup中)findViewById(R.id.rad_btn_group1);
    selectionGroup.setOnCheckedChangeListener(新OnCheckedChangeListener(){        @覆盖
        公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
            // TODO自动生成方法存根
            //因为我们知道radiobutton3是包含了正确的答案:)
            如果(answerAttempt)
            {
                得分 - = 2;
            }
            否则,如果(R.id.radioButton3 == checkedId&放大器;&安培;!answerAttempt)
            {
                ++分;
                answerAttempt = TRUE; //这意味着第一次尝试是由玩家制作
            }
            其他
            {
                 - 得分了;
                answerAttempt = TRUE; //这意味着第一次尝试是由玩家制作
            }        }
    });
}@覆盖
公共无效的onClick(视图v){
    // TODO自动生成方法存根
开关(v.getId())//这是按钮的ID点击(在我们的案例提交按钮)
    {
    案例R.id.button1://这是XML提交按钮的id
        //从这里开始另一项活动上的另一个活动下一个问题:)        打破;
    }}
}

修改

code自动将下一个活动

 电子@覆盖
    公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
        // TODO自动生成方法存根
        //因为我们知道radiobutton3是包含了正确的答案:)
        如果(R.id.radioButton3 == checkedId)
        {
            ++分;
            //转到下一个问题:)
          startActivity(新意图(getBaseContext(),next_question_activity.class));        }
        其他
        {
            //转到下一个问题困扰没有将比分:)
          startActivity(新意图(getBaseContext(),next_question_activity.class));        }    }
});

i am currently doing Android multiple quiz game but i am a bit confused on how to display all the six Questions on a TextView and four Answers on Buttons? also, im not sure how to match the correct/incorrect answer in Android (because it involves Android Syntax).

PS: when player press the button whether its correct of incorrect player will move to the next new questions +answers and would it be possible if i want to store the questions and answers on txt file?

here is what i have done so far...

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


}

解决方案

use Radio buttons for selecting the right answer and use the button to proceed to the next question.!

Advantages

  1. the movement from one activity to other will be more real (as if turning a page)
  2. gives a feeling of moving one level up !! ;)
  3. most important of all easier to implement (well i find it easier that way)

xml Code for the activity layout:

<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 code for the game logic is as follows:

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

}
}

Edit

code for automatically going to next activity

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

        }

    }
});

这篇关于Android上的按钮,TextView的显示器阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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