我怎样才能通过点击Android的一个按钮,改变布局视图? [英] How can I change Views in Layout by clicking a button in Android?

查看:165
本文介绍了我怎样才能通过点击Android的一个按钮,改变布局视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在问答的String [] .The 的TextView 的问题和单选有相应的答案。如果启动按钮被点击时,第一组问题和答案显示在线性布局1 。如果下一个按钮点击 LinearLayout1 由下一组与解答问题的替代,如果再下一个按钮点击下一组问题,必须更换,反之亦然。的帮我写作code为下一个按钮

注意:仅的LinearLayout 1 点击时,应改为下一个按钮

code:

  TextView的电视;
单选按钮RB1,RB2;
按钮bStart,bnext编辑;
字符串的问题[] = {酸橙馅饼是即将推出的版本,
        Android是中间件,这是最新的Andr​​oid版本? };
字符串answerA [] = {真,假};
字符串answerB [] = {真,假};
字符串answerC [] = {的Andr​​oid 4.2,机器人5};@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.ques);
    电视=(的TextView)findViewById(R.id.textView111);
    RB1 =(单选)findViewById(R.id.radioButton1);
    RB2 =(单选)findViewById(R.id.radioButton2);    bStart =(按钮)findViewById(R.id.startExam);
    bnext编辑=(按钮)findViewById(R.id.bNext);
    bStart.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            tv.setText(问题[0]);
            rb1.setText(answerA [0]);
            rb2.setText(answerA [1]);
        }
    });
    bNext.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            tv.setText(的问题[1]);
            rb1.setText(answerB [0]);
            rb2.setText(answerB [1]);        }
    });
}


解决方案

这是一个逻辑的问题。


  1. 合并所有的答案成一个多维数组。

  2. 跟踪当前的问题。

  3. 使用跟踪变量,以获得问题的标题在问题

  4. 使用跟踪变量来得到答案的多维数组中的答案。这是数组的第一个维度。

  5. 您阅读该维度中的答案,并在 RB1​​ RB2
  6. 显示它们

通过使用你有什么有你将不得不延长了code问题。毕竟,你真的要为每个不同的答案的一个新的变量?这一点儿也讲不通。多维数组的作品。

不过......虽然可以提高你的阵列结构,并使用 INT 来跟踪当前显示的问题(从而增加它拿到后,下一个问题pressing的 bnext编辑),我建议你尝试创建更有意义的对象,如问题类,有相关成员(标题 optionA optionB ,等等。 ..),特别是因为它看起来像你使用这个code,以了解如何编写一个测验风格的应用程序。

然后您可以创建各种问题,并通过循环再

但是,这是问题吗?否。问题是跟踪的问题和答案。使用多维数组与跟踪变量。

//编辑:确定,我要建议的东西非常快给你:

 静态的String [] =的问题新的String [] {问题1,问2,问题3};
静态的String [] [] =解答新的String [] [] {
        {答案1.A,答案1.B},
        {答2.A,接听2.B},
        {答3.A,接听3.B},
    };
INT POS = -1;无效showNextAnswer(){
    POS ++;
    tv.setText(问题[POS]);
    rb1.setText(答案[POS] [0]);
    rb2.setText(答案[POS] [1]);
}bStart.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图v){
        showNextAnswer();
    }
});bNext.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图v){
        showNextAnswer();
    }
});

就可以了,两个按钮做同样的事情,所以它可能会更好只使用一个按钮。

I'm taking Questions and Answers in String[].The TextView has questions and RadioButton has corresponding answers. If Start Button is clicked, first set of question and answers is displayed in Linear Layout1. If Next Button is clicked LinearLayout1 is replaced by next set of question with answers and again if Next Buttonis clicked next set of question has to be replaced and vice versa. Help me in writing code for Next Button

Note: Only LinearLayout 1 should be changed when clicking Next Button

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

        }
    });
}

解决方案

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.
  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.

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.

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.

这篇关于我怎样才能通过点击Android的一个按钮,改变布局视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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