如何在字符串数组中添加按钮上的文本

时间:2017-08-26 06:01:29

标签: java android arrays

我正在制作一个测验应用程序,为用户提供了四个选项供选择。我为每个选项创建了一个字符串数组。现在我希望从按钮上的字符串数组中设置文本。我已编写代码但是当我在模拟器上运行它只是显示问题而不是选项按钮。请帮忙!我是初学者。

public class question1 extends AppCompatActivity {

private int Question_no;
private Boolean Boolean_Var;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question1);

    String[] Question_Array = getResources().getStringArray(R.array.Question1);
    TextView Questions = (TextView) findViewById(R.id.Question);
    Questions.setText(Question_Array[Question_no]);

    String[] Option1_Array = getResources().getStringArray(R.array.Option_1);
    Button Option1 = (Button) findViewById(R.id.Option1);
    Option1.setText(Option1_Array[Question_no]);

    String[] Option2_Array = getResources().getStringArray(R.array.Option_2);
    Button Option2 = (Button) findViewById(R.id.Option2);
    Option2.setText(Option2_Array[Question_no]);

    String[] Option3_Array = getResources().getStringArray(R.array.Option_3);
    Button Option3 = (Button) findViewById(R.id.Option3);
    Option3.setText(Option3_Array[Question_no]);

    String[] Option4_Array = getResources().getStringArray(R.array.Option_4);
    Button Option4 = (Button) findViewById(R.id.Option4);
    Option4.setText(Option4_Array[Question_no]);

    findViewById(R.id.Menu_Button).setVisibility(View.INVISIBLE);
    findViewById(R.id.Next_Button).setVisibility(View.INVISIBLE);

}
  

Layout.xml

 <TextView
    android:id="@+id/Question"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:text="TextView"
    android:gravity="center"
    android:textSize="25dp"
    android:textColor="#000000"/>

<Button
    android:id="@+id/Option1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="21dp"
    android:layout_marginRight="21dp"
    android:layout_marginTop="65dp"
    android:text="Button"
    android:textSize="15dp"
    android:textColor="#000000"
    android:layout_below="@+id/Question"
    android:layout_toLeftOf="@+id/Question"
    android:layout_toStartOf="@+id/Question"
    android:onClick="On_Option1_Click"/>

<Button
    android:id="@+id/Option2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/Option1"
    android:layout_marginLeft="19dp"
    android:layout_marginStart="19dp"
    android:textSize="15dp"
    android:layout_toEndOf="@+id/Question"
    android:layout_toRightOf="@+id/Question"
    android:text="Button" />

<Button
    android:id="@+id/Option3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Option1"
    android:layout_alignStart="@+id/Option1"
    android:layout_below="@+id/Option1"
    android:textSize="15dp"
    android:layout_marginTop="49dp"
    android:text="Button" />

<Button
    android:id="@+id/Option4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Option2"
    android:layout_alignStart="@+id/Option2"
    android:layout_alignTop="@+id/Option3"
    android:textSize="15dp"
    android:text="Button" />

<Button
    android:id="@+id/Menu_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Option3"
    android:layout_alignStart="@+id/Option3"
    android:layout_below="@+id/Option3"
    android:layout_marginLeft="37dp"
    android:layout_marginStart="37dp"
    android:layout_marginTop="76dp"
    android:textSize="15dp"
    android:text="MENU" />

<Button
    android:id="@+id/Next_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/Menu_Button"
    android:layout_toEndOf="@+id/Question"
    android:layout_toRightOf="@+id/Question"
    android:textSize="15dp"
    android:text="NEXT" />
  

的strings.xml

<string-array name="Question1">
    <item> which of the foll. is a non-metal that remains liquid at room temp? </item>
</string-array>

<string-array name="Option_1">
    <item>Phosphorous</item>
</string-array>

<string-array name="Option_2">
    <item>Bromine</item>
</string-array>

<string-array name="Option_3">
    <item>Chlorine</item>
</string-array>

<string-array name="Option_4">
    <item>Helium</item>
</string-array>

<string-array name="Answer1">
    <item>Bromine</item>
</string-array>

1 个答案:

答案 0 :(得分:0)

好的,因为我无法看到你的布局或字符串xml文件,所以无法确定你的问题。但是,我已经为您编写并测试了这个,并且它有效。我还没有完全按照您的确切实施,但它并不遥远,希望它能帮助您学习阅读并遵循此代码。我并不是说这是通过任何方式做到这一点的最佳方式,而且我确实在一小时左右就把它一起砍掉了,但它确实有效,并且它并不是非常难看: - )< / p>

的strings.xml:

    <resources>
        <string name="app_name">AndroidApp</string>
        <string-array name="questions">
            <item>(1) Which is your favourite of these colours?</item>
            <item>(2) Which is your favourite of these animals?</item>
        </string-array>
        <string-array name="firstOptions">
            <item>Blue</item>
            <item>Red</item>
            <item>Yellow</item>
            <item>Green</item>
        </string-array>
        <string-array name="secondOptions">
            <item>Dog</item>
            <item>Cat</item>
            <item>Gerbil</item>
            <item>Bearded Dragon</item>
        </string-array>
    </resources>

layout.xml:

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apps.radley.mike.androidapp.MainActivity">

         <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Placeholder"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/questionView"
    app:layout_constraintVertical_bias="0.1" />

        <Button
    android:id="@+id/firstButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonClicked"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.4" />

        <Button
    android:id="@+id/secondButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonClicked"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.5" />

        <Button
    android:id="@+id/thirdButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonClicked"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.6" />

        <Button
    android:id="@+id/fourthButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="buttonClicked"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.7" />


    </android.support.constraint.ConstraintLayout>

MainActivity.Java:

    package com.apps.radley.mike.androidapp;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {

        private int questionIndex;

        private List<Button> buttons;

        private List<List<String>> options;

        private List<String> theQuestions;
        private List<String> firstOptionList;
        private List<String> secondOptionList;

        private StringBuilder answers;

        private TextView questionView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initVariables();
            initButtons();
            initOptions();
            updateView();
        }

        private void initVariables() {
            questionIndex = 0;
            buttons = new ArrayList<>();
            options = new ArrayList<>();
            theQuestions = Arrays.asList(getResources().getStringArray(R.array.questions));
            firstOptionList = Arrays.asList(getResources().getStringArray(R.array.firstOptions));
            secondOptionList = Arrays.asList(getResources().getStringArray(R.array.secondOptions));
            answers = new StringBuilder("Answers: ");
            questionView = (TextView) findViewById(R.id.questionView);
        }

        private void initOptions() {
            options.add(firstOptionList);
            options.add(secondOptionList);
        }

        private void updateView() {
            questionView.setText(theQuestions.get(questionIndex));
            setButtonText();
        }

        private void setButtonText() {
            for (int i = 0; i < buttons.size(); i++) {
                buttons.get(i).setText(options.get(questionIndex).get(i));
            }
        }

        private void initButtons() {
            buttons.add((Button) findViewById(R.id.firstButton));
            buttons.add((Button) findViewById(R.id.secondButton));
            buttons.add((Button) findViewById(R.id.thirdButton));
            buttons.add((Button) findViewById(R.id.fourthButton));
        }

        private void showFinalView() {
            questionView.setText(answers.toString());
            for (Button button : buttons) {
                button.setVisibility(View.INVISIBLE);
            }
        }

        public void buttonClicked(View aView){
            answers.append(((Button) aView).getText());
            answers.append("; ");
            if (questionIndex < 1) {
                questionIndex += 1;
                updateView();
            } else {
                // We've run out of questions so...
                showFinalView();
            }
        }
    }

如果你检查了你得到了什么并将其与之比较,和/或运行它并玩它并操纵它以实现自己的目标,我认为你能够离开你当前的quandry。享受乐趣和快乐的学习: - )

编辑:我也运行你的代码。你的代码确实有效。只是你的按钮都是相互叠加的,因为你没有设置它们的约束。将您的布​​局文件更改为以下内容,您将看到:

    <TextView
    android:id="@+id/Question"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:text="TextView"
    android:gravity="center"
    android:textSize="25dp"
    android:textColor="#000000"/>

    <Button
    android:id="@+id/Option1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="21dp"
    android:layout_marginRight="21dp"
    android:layout_marginTop="65dp"
    android:text="Button"
    android:textSize="15dp"
    android:textColor="#000000"
    android:layout_below="@+id/Question"
    android:layout_toLeftOf="@+id/Question"
    android:layout_toStartOf="@+id/Question"
    android:onClick="On_Option1_Click"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.4" />

    <Button
    android:id="@+id/Option2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/Option1"
    android:layout_marginLeft="19dp"
    android:layout_marginStart="19dp"
    android:textSize="15dp"
    android:layout_toEndOf="@+id/Question"
    android:layout_toRightOf="@+id/Question"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.6" />

    <Button
    android:id="@+id/Option3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Option1"
    android:layout_alignStart="@+id/Option1"
    android:layout_below="@+id/Option1"
    android:textSize="15dp"
    android:layout_marginTop="49dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.7" />

    <Button
    android:id="@+id/Option4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Option2"
    android:layout_alignStart="@+id/Option2"
    android:layout_alignTop="@+id/Option3"
    android:textSize="15dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.9" />

    <Button
    android:id="@+id/Menu_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Option3"
    android:layout_alignStart="@+id/Option3"
    android:layout_below="@+id/Option3"
    android:layout_marginLeft="37dp"
    android:layout_marginStart="37dp"
    android:layout_marginTop="76dp"
    android:textSize="15dp"
    android:text="MENU" />

    <Button
    android:id="@+id/Next_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/Menu_Button"
    android:layout_toEndOf="@+id/Question"
    android:layout_toRightOf="@+id/Question"
    android:textSize="15dp"
    android:text="NEXT" />  

无论如何,这肯定适用于我的机器:-)(虽然我也正确检查了布局设置,因为我还没有做很多事情,但是复制并粘贴视图设置在那里让它们到进入视野)。