简体   繁体   English

如何通过单击Android中的按钮来更改布局中的视图?

[英]How can I change Views in Layout by clicking a button in Android?

我的要求

I'm taking Questions and Answers in String[] .The TextView has questions and RadioButton has corresponding answers. 我正在使用String[]问题和答案TextView有问题, RadioButton有相应的答案。 If Start Button is clicked, first set of question and answers is displayed in Linear Layout1 . 如果Start Button ,则在Linear Layout1显示第一组问题和答案。 If Next Button is clicked LinearLayout1 is replaced by next set of question with answers and again if Next Button is clicked next set of question has to be replaced and vice versa. 如果单击了“下Next Button则用下一个带有答案的问题集替换LinearLayout1 ,如果再次单击“下Next Button则必须替换下一个问题集,反之亦然。 Help me in writing code for Next Button 帮助我编写Next Button代码

Note : Only LinearLayout 1 should be changed when clicking Next Button 注意 :单击“ Next Button时,仅应更改LinearLayout 1

code: 码:

TextView tv;
RadioButton rb1, rb2;
Button bStart, bNext;
String question[] = { "Key Lime Pie is upcoming version",
        "Android is Middleware", "Which is latest Android version?" };
String answerA[] = { "True", "False" };
String answerB[] = { "True", "False" };
String answerC[] = { "Android 4.2", "Android 5" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ques);
    tv = (TextView) findViewById(R.id.textView111);
    rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb2 = (RadioButton) findViewById(R.id.radioButton2);

    bStart = (Button) findViewById(R.id.startExam);
    bNext = (Button) findViewById(R.id.bNext);
    bStart.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv.setText(question[0]);
            rb1.setText(answerA[0]);
            rb2.setText(answerA[1]);
        }
    });
    bNext.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv.setText(question[1]);
            rb1.setText(answerB[0]);
            rb2.setText(answerB[1]);

        }
    });
}

Create a single array for answers. 创建一个答案数组。 And declare tracking variables for questions and answers as: 并声明跟踪变量的问题和答案为:

  String answer[] = {"True", "False" , "True", "False", 
                     "Android 4.2", "Android 5" };
  int current = 0; // this is your new variable
  int ans = 1;     // this is initialized to 1, because 
                   // I assumed your first question and answer
                   // is displayed with the press of start button

Change your next button listener to this 将您的下一个按钮侦听器更改为此

public void onClick(View v) {
        current ++;
        if(current < question.length){
            tv.setText(question[current]);
            rb1.setText(answer[ans+=1]);
            rb2.setText(answer[ans+=1]);
        }

    }

This is a logic problem. 这是一个逻辑问题。

  1. Merge all the answers into a multidimensional array. 将所有答案合并到多维数组中。
  2. Keep track of the current question. 跟踪当前问题。
  3. Use the tracking variable to get the question title in question . 使用跟踪变量获得在问题的标题question
  4. Use the tracking variable to get the answers in the multidimensional array of answers. 使用跟踪变量来获取多维答案数组中的答案。 This is the first dimension of the array. 这是数组的第一维。
  5. You read the answers in that dimension and display them in rb1 and rb2 . 您阅读该维度的答案,并在rb1rb2显示它们。

By using what you have there you will have problems extending that code. 通过使用那里的内容,您将无法扩展该代码。 After all, are you really going to create a new variable for each different set of answers? 毕竟,您真的要为每个不同的答案集创建一个新变量吗? It does not make any sense. 这没有任何意义。 A multidimensional array works. 多维数组有效。

However... although you can improve your arrays structure and use an int to keep track of the current displayed question (and thus increment it to get the next question after pressing the bNext ), I suggest you try to create more meaningful objects, like a Question class, with relevant members ( title , optionA , optionB , whatever...), especially because it looks like you're using this code to learn how to program a quiz-styled application. 但是...尽管您可以改善数组结构并使用int跟踪当前显示的问题(并在按bNext后增加它以得到下一个问题),但我建议您尝试创建更有意义的对象,例如一个Question类,具有相关的成员( titleoptionAoptionB等等),尤其是因为您似乎正在使用此代码来学习如何编写测验样式的应用程序。

Then you can create various questions and iterate through then. 然后,您可以创建各种问题并进行迭代。

But is that the issue there? 但这是那里的问题吗? No. The problem is keeping track of questions and answers. 否。问题在于跟踪问题和答案。 Use a multidimensional array with a tracking variable. 使用具有跟踪变量的多维数组。

// edited: OK, I'm going to suggest something very quick for you: //编辑:好的,我将为您提供一些快速建议:

static String[] questions = new String[] { "Question 1", "Question 2", "Question 3" };
static String[][] answers = new String[][] {
        {"Answer 1.a", "Answer 1.b"},
        {"Answer 2.a", "Answer 2.b"},
        {"Answer 3.a", "Answer 3.b"},
    };
int pos = -1;

void showNextAnswer() {
    pos++;
    tv.setText(questions[pos]);
    rb1.setText(answers[pos][0]);
    rb2.setText(answers[pos][1]);
}

bStart.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        showNextAnswer();
    }
});

bNext.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        showNextAnswer();
    }
});

As you can, both buttons do the same thing, so it's probably better to only use a next button. 您可以,两个按钮都做相同的事情,因此最好只使用下一个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM