使用TextView的阵列的应用调查问卷 [英] Using textView arrays for questionnaire app

查看:161
本文介绍了使用TextView的阵列的应用调查问卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立与单独显示和问题的调查问卷类型的应用程序,当用户提交一个答案,点击数据被存储在上面一个TextView的按钮和问题要问在哪里这个问题的答案存储在一个问题一个新的TextView。我有困难得到改变下一个问题的问题。

I want to build a questionnaire type app with questions that show up individually and when a user submits an answer and clicks the button the data is stored in a textView above and the question goes to the next question where that answer is stored in a new textView. I am having difficulty getting the question to change to the next question.

下面我用数组方法尝试,但已经得到了错误都在这里

Below I have attempted using the array method but have gotten errors both here

myTexts[0]=findViewById(R.id.question1);
myTexts[1]=findViewById(R.id.question2); 

- 和

 myTexts[questionNumber].setText(mEdit.getText().toString());
 questionNumber++;

下面是完整的源$ C ​​$ C:

Below is complete source code:

package com.example.greg;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class menu extends Activity {

Button   mButton;
EditText mEdit;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.userAnswereditText);

    TextView [] myTexts = new TextView[2];
    myTexts[0]=findViewById(R.id.question1);
    myTexts[1]=findViewById(R.id.question2);

    int questionNumber = 0;
    mButton.setOnClickListener(
    new View.OnClickListener()
       {
            public void onClick(View view)
            {

                myTexts[questionNumber].setText(mEdit.getText().toString());
                questionNumber++;   
            }
        });


}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

推荐答案

要改变你应该有字符串或字符串列表的数组,可以容纳所有的问题,只要点击按钮,答案存储在问题TextView中要加载从字符串数组或列表持有该问题,下一个问题。

To change question you should have array of String or List of String that can hold all the question and as soon as button is clicked and the answer is stored in the TextView you will load the next question from that array or List of String that is holding the questions.

 String [] questions;
 int numberOfQuestions = 2;

,然后在onCreate()方法

and then in onCreate() method

 questions=new String[numberOfQuestions];//numberOfQuestion refers to integer that holds total number of questions
 questions[0]="This is first question?";
 questions[1]="This is second question?";

和现在你view.onClickListener

and now in your view.onClickListener

mButton.setOnClickListener(
new View.OnClickListener()
   {
        public void onClick(View view)
        {

            myTexts[questionNumber].setText(mEdit.getText().toString());
            questionNumber++;   
            //here you will have to load the next question 
            if(questionNumber < numberOfQuestions)
                questionTextViewHolder.setText(questions[questionNumber]);
            else
                Toast.makeText(menu.this,"No more questions!",Toast.LENGTHLONG).show();

            //Note one thing that your questionNumber variable should not increase myTexts length or questions length because if it does you will get  exception
        }
    });

questionTextViewHolder是TextView的您在其中显示的问题就是要回答,你将与你的TextView您在其中显示的问题,以取代questionTextViewHolder!
类名菜单首字母应该大写,如果你遵循的编码约定。

questionTextViewHolder is the TextView in which you are displaying the question that is to be answered, you will have to replace "questionTextViewHolder " with your textview in which you are displaying the questions ! Class name menu starting letter should be capital if you follow the coding conventions.

这篇关于使用TextView的阵列的应用调查问卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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